修改字符模块

This commit is contained in:
MuHua-123
2025-06-07 16:10:24 +08:00
parent 937595b3b1
commit dedc290469
64 changed files with 636 additions and 889 deletions
@@ -1,46 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作动画控制器
/// </summary>
[RequireComponent(typeof(Animator))]
public class KinesisAnimator : MonoBehaviour {
protected int layerIndex;
protected string current;
protected Animator animator;
protected IKinesis kinesis;
public virtual void Awake() => animator = GetComponent<Animator>();
/// <summary> 设置动作 </summary>
public virtual void SetKinesis(IKinesis kinesis) => this.kinesis = kinesis;
/// <summary> 动画过渡 </summary>
public virtual void Transition(int layerIndex, string name, float normalizedTransitionDuration = 0.1f) {
animator.SetLayerWeight(this.layerIndex, 0);
animator.SetLayerWeight(layerIndex, 1);
this.layerIndex = layerIndex;
Transition(name, normalizedTransitionDuration);
}
/// <summary> 动画过渡 </summary>
public virtual void Transition(string name, float normalizedTransitionDuration = 0.1f) {
if (current == name) { animator.Play(name); }
else { animator.CrossFade(name, normalizedTransitionDuration); }
current = name;
}
/// <summary> 设置参数 </summary>
public virtual void SetBool(string name, bool value) => animator.SetBool(name, value);
/// <summary> 设置参数 </summary>
public virtual void SetFloat(string name, float value) => animator.SetFloat(name, value);
/// <summary> 触发动画特效 </summary>
public virtual void AnimationEffects() => kinesis?.AnimationEffects();
/// <summary> 动画结束(有后摇) </summary>
public virtual void AnimationEnd() => kinesis?.AnimationEnd();
/// <summary> 动画退出(无后摇) </summary>
public virtual void AnimationExit() => kinesis?.AnimationExit();
}
}
@@ -1,32 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作控制器
/// </summary>
public class KinesisController : MonoBehaviour {
public KinesisAnimator animator;
public KinesisMovement movement;
private IKinesis currentKinesis;
public virtual void Awake() => TransitionKinesis(new KinesisIdle());
public virtual void Update() => currentKinesis?.UpdateKinesis();
/// <summary> 动作过渡 </summary>
public virtual void TransitionKinesis(IKinesis kinesis) {
//不可以转换
if (currentKinesis != null && !currentKinesis.Transition(kinesis)) { return; }
//进行转换
currentKinesis?.FinishKinesis();
currentKinesis = kinesis;
currentKinesis?.StartKinesis();
animator?.SetKinesis(currentKinesis);
movement?.SetKinesis(currentKinesis);
}
}
}
@@ -1,100 +0,0 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 动作运动控制器
/// </summary>
[RequireComponent(typeof(CharacterController))]
public class KinesisMovement : MonoBehaviour {
public float moveSpeed = 5;// 移动速度
public float acceleration = 10.0f;// 加速度
[Range(0.0f, 0.3f)]
public float rotationSmoothTime = 0.12f;// 旋转平滑
protected float currentSpeed;// 当前速度
protected Vector2 moveDirection;// 移动方向
protected float animationBlend;// 动画混合速度
protected float targetRotation = 0.0f;// 旋转目标
protected float rotationVelocity;// 旋转速度
protected float verticalVelocity;// 垂直速度
protected IKinesis kinesis;// 当前动作
protected KinesisAnimator animator;// 动作动画控制器
protected CharacterController controller;// 角色控制器
public virtual bool IsStop => currentSpeed == 0;
public virtual void Awake() {
animator = GetComponent<KinesisAnimator>();
controller = GetComponent<CharacterController>();
}
public virtual void Update() {
PlanarMovement();
}
/// <summary> 设置动作 </summary>
public virtual void SetKinesis(IKinesis kinesis) => this.kinesis = kinesis;
/// <summary> 设置方向 </summary>
public virtual void SetDirection(Vector2 moveDirection) => this.moveDirection = moveDirection;
/// <summary> 停止移动 </summary>
public virtual void StopMovement() {
currentSpeed = 0;
moveDirection = Vector2.zero;
animationBlend = 0;
animator?.SetFloat("MoveSpeed", animationBlend);
}
/// <summary> 平面移动 </summary>
public virtual void PlanarMovement() {
// 设定目标速度
float targetSpeed = moveSpeed;
// 一种简单的加速和减速设计,易于拆卸、更换或迭代
// 如果没有输入,将目标速度设置为0
if (moveDirection == Vector2.zero && currentSpeed == 0) { return; }
if (moveDirection == Vector2.zero) targetSpeed = 0.0f;
// 当前水平速度的参考
// float currentHorizontalSpeed = new Vector3(controller.velocity.x, 0.0f, controller.velocity.z).magnitude;
// float speedOffset = 0.1f;
// 加速或减速至目标速度
// if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset) {
// 产生弯曲的结果,而不是线性的结果,从而产生更有机的速度变化
// 注意Lerp中的T是夹紧的,所以我们不需要夹紧我们的速度
currentSpeed = Mathf.Lerp(currentSpeed, targetSpeed, Time.deltaTime * acceleration);
// round speed to 3 decimal places
currentSpeed = Mathf.Round(currentSpeed * 1000f) / 1000f;
// }
// else { currentSpeed = targetSpeed; }
animationBlend = Mathf.Lerp(animationBlend, targetSpeed, Time.deltaTime * acceleration);
if (animationBlend < 0.01f) animationBlend = 0f;
// 使输入方向标准化
Vector3 inputDirection = new Vector3(moveDirection.x, 0.0f, moveDirection.y).normalized;
// 如果有移动输入,则在玩家移动时旋转玩家
if (moveDirection != Vector2.zero) {
targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref rotationVelocity, rotationSmoothTime);
// 相对于相机位置旋转到面向输入方向
transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
}
Vector3 targetDirection = Quaternion.Euler(0.0f, targetRotation, 0.0f) * Vector3.forward;
// 移动
controller.Move(targetDirection.normalized * (currentSpeed * Time.deltaTime) + new Vector3(0.0f, verticalVelocity, 0.0f) * Time.deltaTime);
// 如果使用角色,请更新动画师
animator?.SetFloat("MoveSpeed", animationBlend);
}
}
}
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 运动器
/// </summary>
public abstract class Movement {
/// <summary> 当前速度 </summary>
public abstract float Speed { get; }
/// <summary> 是否接地 </summary>
public abstract bool Grounded { get; }
/// <summary> 移动 </summary>
public abstract void Move(Vector2 moveDirection, float moveSpeed, float acceleration, bool isRotation);
/// <summary> 跳跃 </summary>
public abstract void Jump(float jumpHeight);
/// <summary> 更新 </summary>
public abstract void Update();
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 61ef13b926e462442920d0ba932e8e27
guid: c48241a855a542e41b23b017edcab454
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,109 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 碰撞 - 运动器
/// </summary>
public class MovementCollision : Movement {
/// <summary> 地面图层 </summary>
public readonly LayerMask groundLayers;
/// <summary> 角色控制器 </summary>
public readonly CharacterController controller;
public float moveSpeed;// 移动速度
public float currentSpeed;// 当前速度
public float acceleration;// 加速度
public float animationBlend;// 动画混合速度
public Vector2 moveDirection;// 移动方向
public bool isRotation;// 是否旋转
public float targetRotation;// 目标旋转
public float rotationVelocity;// 旋转速度
public float rotationSmoothTime = 0.12f;// 旋转平滑 Range(0.0f, 0.3f)
public bool grounded = true;// 是否接地
public float verticalVelocity;// 垂直速度
public float groundedRadius = 0.14f;// 地面检测半径
/// <summary> 垂直重力 </summary>
public float Gravity => Physics.gravity.y;
public override float Speed => currentSpeed;
public override bool Grounded => grounded;
public MovementCollision(CharacterController controller, LayerMask groundLayers) {
this.controller = controller;
this.groundLayers = groundLayers;
}
/// <summary> 移动 </summary>
public override void Move(Vector2 moveDirection, float moveSpeed, float acceleration, bool isRotation) {
this.moveSpeed = moveSpeed;
this.acceleration = acceleration;
this.moveDirection = moveDirection;
this.isRotation = isRotation;
}
/// <summary> H*-2*G的平方根=达到所需高度所需的速度 </summary>
public override void Jump(float jumpHeight) {
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * Gravity);
}
/// <summary> 更新 </summary>
public override void Update() {
// 如果没有输入,将目标速度设置为0
if (moveDirection == Vector2.zero) moveSpeed = 0.0f;
// 当前速度
currentSpeed = Mathf.Lerp(currentSpeed, moveSpeed, Time.deltaTime * acceleration);
// 四舍五入到小数点后3位
currentSpeed = Mathf.Round(currentSpeed * 1000f) / 1000f;
animationBlend = Mathf.Lerp(animationBlend, moveSpeed, Time.deltaTime * acceleration);
if (animationBlend < 0.01f) animationBlend = 0f;
// 使输入方向标准化
Vector3 inputDirection = new Vector3(moveDirection.x, 0.0f, moveDirection.y).normalized;
if (isRotation) {
// 如果有移动输入,则在玩家移动时旋转玩家
if (moveDirection != Vector2.zero) {
targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
float rotation = Mathf.SmoothDampAngle(controller.transform.eulerAngles.y, targetRotation, ref rotationVelocity, rotationSmoothTime);
// 相对于相机位置旋转到面向输入方向
controller.transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
}
// 移动
Vector3 targetDirection = Quaternion.Euler(0.0f, targetRotation, 0.0f) * Vector3.forward;
Vector3 horizontal = targetDirection.normalized * (currentSpeed * Time.deltaTime);
Vector3 vertical = new Vector3(0.0f, verticalVelocity, 0.0f) * Time.deltaTime;
controller.Move(horizontal + vertical);
}
else {
// 移动
Vector3 horizontal = inputDirection * (currentSpeed * Time.deltaTime);
Vector3 vertical = new Vector3(0.0f, verticalVelocity, 0.0f) * Time.deltaTime;
controller.Move(horizontal + vertical);
}
// 地面检测
Vector3 position = controller.transform.position;
Vector3 rayOrigin = new Vector3(position.x, position.y + groundedRadius, position.z);
// 射线长度稍微大于检测半径
float rayLength = groundedRadius * 2 + 0.1f;
// 使用射线检测地面
grounded = Physics.Raycast(rayOrigin, Vector3.down, rayLength, groundLayers, QueryTriggerInteraction.Ignore);
// 可选:调试显示射线
Debug.DrawRay(rayOrigin, Vector3.down * rayLength, grounded ? Color.green : Color.red);
// 引力
verticalVelocity += Gravity * Time.deltaTime;
// 站在地面上时,限制最大下落速度
if (grounded && verticalVelocity < 0.0f) { verticalVelocity = -2f; }
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e3ceef7224b9b094aadaa9b3a0238648
guid: 06f3697338f20ac459ed126fc05a9694
MonoImporter:
externalObjects: {}
serializedVersion: 2
@@ -0,0 +1,101 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MuHua {
/// <summary>
/// 标准 - 运动器
/// </summary>
public class MovementStandard : Movement {
/// <summary> 地面图层 </summary>
public readonly LayerMask groundLayers;
/// <summary> 变换 </summary>
public readonly Transform transform;
public float moveSpeed;// 移动速度
public float currentSpeed;// 当前速度
public float acceleration;// 加速度
public float animationBlend;// 动画混合速度
public Vector2 moveDirection;// 移动方向
public bool isRotation;// 是否旋转
public float targetRotation;// 目标旋转
public float rotationVelocity;// 旋转速度
public float rotationSmoothTime = 0.12f;// 旋转平滑 Range(0.0f, 0.3f)
public bool grounded = true;// 是否接地
public float verticalVelocity;// 垂直速度
public float groundedRadius = 0.14f;// 地面检测半径
/// <summary> 垂直重力 </summary>
public float Gravity => Physics.gravity.y;
public override float Speed => currentSpeed;
public override bool Grounded => grounded;
public MovementStandard(Transform transform, LayerMask groundLayers) {
this.transform = transform;
this.groundLayers = groundLayers;
}
/// <summary> 移动 </summary>
public override void Move(Vector2 moveDirection, float moveSpeed, float acceleration, bool isRotation) {
this.moveSpeed = moveSpeed;
this.acceleration = acceleration;
this.moveDirection = moveDirection;
this.isRotation = isRotation;
}
/// <summary> 跳跃 </summary>
public override void Jump(float jumpHeight) {
verticalVelocity = Mathf.Sqrt(jumpHeight * -2f * Gravity);
}
/// <summary> 更新 </summary>
public override void Update() {
// 如果没有输入,将目标速度设置为0
if (moveDirection == Vector2.zero) moveSpeed = 0.0f;
// 当前速度
currentSpeed = Mathf.Lerp(currentSpeed, moveSpeed, Time.deltaTime * acceleration);
// 四舍五入到小数点后3位
currentSpeed = Mathf.Round(currentSpeed * 1000f) / 1000f;
animationBlend = Mathf.Lerp(animationBlend, moveSpeed, Time.deltaTime * acceleration);
if (animationBlend < 0.01f) animationBlend = 0f;
// 使输入方向标准化
Vector3 inputDirection = new Vector3(moveDirection.x, 0.0f, moveDirection.y).normalized;
// 如果有移动输入,则在玩家移动时旋转玩家
if (moveDirection != Vector2.zero && isRotation) {
targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg;
float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref rotationVelocity, rotationSmoothTime);
// 相对于相机位置旋转到面向输入方向
transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f);
}
// 移动
Vector3 targetDirection = Quaternion.Euler(0.0f, targetRotation, 0.0f) * Vector3.forward;
Vector3 horizontal = targetDirection.normalized * (currentSpeed * Time.deltaTime);
Vector3 vertical = new Vector3(0.0f, verticalVelocity, 0.0f) * Time.deltaTime;
transform.position += horizontal + vertical;
// 地面检测
Vector3 position = transform.position;
Vector3 rayOrigin = new Vector3(position.x, position.y + groundedRadius, position.z);
// 射线长度稍微大于检测半径
float rayLength = groundedRadius * 2 + 0.1f;
// 使用射线检测地面
grounded = Physics.Raycast(rayOrigin, Vector3.down, rayLength, groundLayers, QueryTriggerInteraction.Ignore);
// 可选:调试显示射线
Debug.DrawRay(rayOrigin, Vector3.down * rayLength, grounded ? Color.green : Color.red);
// 引力
verticalVelocity += Gravity * Time.deltaTime;
// 站在地面上时,限制最大下落速度
if (grounded && verticalVelocity < 0.0f) { verticalVelocity = -2f; }
}
}
}
@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 2c1979e13b26c7b48a7a4690da154535
guid: cf671b0b45929dc4c970eadb97df3ec8
MonoImporter:
externalObjects: {}
serializedVersion: 2