diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-22 12:14:00 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-22 12:14:00 +0800 |
| commit | 9045525044dd1ab38a559598bea54858f4050c49 (patch) | |
| tree | 2dc94de7ee2eedadf5b5509bbfe211fab36fb284 /Assets/Scripts | |
| parent | 11d02a4cc3ce2d1df6dbfc9ce33e0d8649109382 (diff) | |
增加使用RootMotion驱动的角色移动
Diffstat (limited to 'Assets/Scripts')
| -rw-r--r-- | Assets/Scripts/AnimatorHandler.cs | 16 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerLocomotion.cs | 25 |
2 files changed, 39 insertions, 2 deletions
diff --git a/Assets/Scripts/AnimatorHandler.cs b/Assets/Scripts/AnimatorHandler.cs index d95d12b..b71c573 100644 --- a/Assets/Scripts/AnimatorHandler.cs +++ b/Assets/Scripts/AnimatorHandler.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -6,6 +7,7 @@ namespace DS { public class AnimatorHandler : MonoBehaviour { + public PlayerLocomotion playerLocomotion; public Animator animator; private int _vertical; private int _horizontal; @@ -87,6 +89,20 @@ namespace DS { canRotate = false; } + + private void OnAnimatorMove() + { + Debug.Log(animator.velocity); + playerLocomotion.rootMotion = animator.velocity; + } + + private void OnDrawGizmos() + { + // 我们先假定 地板永远是朝上的 + var normalVector = Vector3.up; + Vector3 rootMotionProjected = Vector3.ProjectOnPlane(animator.velocity, normalVector); + Gizmos.DrawLine(transform.position, transform.position + rootMotionProjected * 2f); + } } } diff --git a/Assets/Scripts/PlayerLocomotion.cs b/Assets/Scripts/PlayerLocomotion.cs index aaa0c12..9f058b0 100644 --- a/Assets/Scripts/PlayerLocomotion.cs +++ b/Assets/Scripts/PlayerLocomotion.cs @@ -9,6 +9,16 @@ namespace DS { public class PlayerLocomotion : MonoBehaviour { + // 是否使用根动画 + [Range(0, 1)] public float rootMotionBlending = 1f; + + public bool usingRootMotion + { + get => rootMotionBlending > 0.5f; + set => rootMotionBlending = value ? 0 : 1; + } + + [HideInInspector] public Vector3 rootMotion; private Transform _cameraObject; //存储Camera的位置 private InputHandler _inputHandler; private Vector3 _moveDirection; @@ -35,6 +45,16 @@ namespace DS private void Update() { + UpdateCharacterMovement(); + } + + private void FixedUpdate() + { + + } + + private void UpdateCharacterMovement() + { float delta = Time.deltaTime; _inputHandler.TickInput(delta); @@ -45,8 +65,9 @@ namespace DS float speed = movementSpeed; _moveDirection *= speed; - - Vector3 projectedVelocity = Vector3.ProjectOnPlane(_moveDirection, _normalVector); + + var baseVelocity = Vector3.Lerp(rootMotion, _moveDirection, rootMotionBlending); + Vector3 projectedVelocity = Vector3.ProjectOnPlane(baseVelocity, _normalVector); rigidbody.velocity = projectedVelocity; animatorHandler.UpdateAnimatorValues(_inputHandler.moveAmount,0); |
