From 9045525044dd1ab38a559598bea54858f4050c49 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Fri, 22 May 2026 12:14:00 +0800 Subject: 增加使用RootMotion驱动的角色移动 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/PlayerLocomotion.cs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'Assets/Scripts/PlayerLocomotion.cs') 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; @@ -34,6 +44,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); -- cgit