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 | |
| parent | 11d02a4cc3ce2d1df6dbfc9ce33e0d8649109382 (diff) | |
增加使用RootMotion驱动的角色移动
Diffstat (limited to 'Assets')
| -rw-r--r-- | Assets/Art/Animator/Humanoid.controller | 89 | ||||
| -rw-r--r-- | Assets/Assets/polygon_assets/PolygonFantasyHeroCharacters/URP_Extract_Me.unitypackage.meta | 7 | ||||
| -rw-r--r-- | Assets/Scenes/SampleScene.unity | 8 | ||||
| -rw-r--r-- | Assets/Scripts/AnimatorHandler.cs | 16 | ||||
| -rw-r--r-- | Assets/Scripts/PlayerLocomotion.cs | 25 |
5 files changed, 141 insertions, 4 deletions
diff --git a/Assets/Art/Animator/Humanoid.controller b/Assets/Art/Animator/Humanoid.controller index 62b6f12..ab72720 100644 --- a/Assets/Art/Animator/Humanoid.controller +++ b/Assets/Art/Animator/Humanoid.controller @@ -1,5 +1,27 @@ %YAML 1.1 %TAG !u! tag:unity3d.com,2011: +--- !u!1101 &-2413022822332482909 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: [] + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: 7601682898943563198} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.8611111 + m_HasExitTime: 1 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 --- !u!91 &9100000 AnimatorController: m_ObjectHideFlags: 0 @@ -21,6 +43,12 @@ AnimatorController: m_DefaultInt: 0 m_DefaultBool: 0 m_Controller: {fileID: 9100000} + - m_Name: DEBUG_Roll + m_Type: 9 + m_DefaultFloat: 0 + m_DefaultInt: 0 + m_DefaultBool: 0 + m_Controller: {fileID: 9100000} m_AnimatorLayers: - serializedVersion: 5 m_Name: Base Layer @@ -73,6 +101,34 @@ BlendTree: m_UseAutomaticThresholds: 1 m_NormalizedBlendValues: 0 m_BlendType: 3 +--- !u!1102 &5025552237477777202 +AnimatorState: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: mixamo_com + m_Speed: 1 + m_CycleOffset: 0 + m_Transitions: + - {fileID: -2413022822332482909} + m_StateMachineBehaviours: [] + m_Position: {x: 50, y: 50, z: 0} + m_IKOnFeet: 0 + m_WriteDefaultValues: 1 + m_Mirror: 0 + m_SpeedParameterActive: 0 + m_MirrorParameterActive: 0 + m_CycleOffsetParameterActive: 0 + m_TimeParameterActive: 0 + m_Motion: {fileID: -203655887218126122, guid: 442d2cd7da7e6f947804c626e4014efb, + type: 3} + m_Tag: + m_SpeedParameter: + m_MirrorParameter: + m_CycleOffsetParameter: + m_TimeParameter: --- !u!1107 &7200939722294922558 AnimatorStateMachine: serializedVersion: 6 @@ -84,7 +140,10 @@ AnimatorStateMachine: m_ChildStates: - serializedVersion: 1 m_State: {fileID: 7601682898943563198} - m_Position: {x: 280, y: 70, z: 0} + m_Position: {x: 30, y: 270, z: 0} + - serializedVersion: 1 + m_State: {fileID: 5025552237477777202} + m_Position: {x: 330, y: 120, z: 0} m_ChildStateMachines: [] m_AnyStateTransitions: [] m_EntryTransitions: [] @@ -105,7 +164,8 @@ AnimatorState: m_Name: Locomotion m_Speed: 1 m_CycleOffset: 0 - m_Transitions: [] + m_Transitions: + - {fileID: 8528838308025900624} m_StateMachineBehaviours: [] m_Position: {x: 50, y: 50, z: 0} m_IKOnFeet: 0 @@ -121,3 +181,28 @@ AnimatorState: m_MirrorParameter: m_CycleOffsetParameter: m_TimeParameter: +--- !u!1101 &8528838308025900624 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: + - m_ConditionMode: 1 + m_ConditionEvent: DEBUG_Roll + m_EventTreshold: 0 + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: 5025552237477777202} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.7494432 + m_HasExitTime: 0 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 diff --git a/Assets/Assets/polygon_assets/PolygonFantasyHeroCharacters/URP_Extract_Me.unitypackage.meta b/Assets/Assets/polygon_assets/PolygonFantasyHeroCharacters/URP_Extract_Me.unitypackage.meta new file mode 100644 index 0000000..c270ee8 --- /dev/null +++ b/Assets/Assets/polygon_assets/PolygonFantasyHeroCharacters/URP_Extract_Me.unitypackage.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 49d02ab3e50665641baeb9e39ed01cca +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index f708fd0..b15a6eb 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -1143,6 +1143,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 4ca3b9320dac3304ba425bd0d3952131, type: 3} m_Name: m_EditorClassIdentifier: + rootMotionBlending: 0 + rootMotion: {x: 0, y: 0, z: 0} myTransform: {fileID: 0} animatorHandler: {fileID: 0} rigidbody: {fileID: 0} @@ -2484,6 +2486,11 @@ PrefabInstance: propertyPath: m_Controller value: objectReference: {fileID: 9100000, guid: 8ceb66b9d11550e4cb0f7af887ee0d29, type: 2} + - target: {fileID: 95506792919036566, guid: bbf9d2d73db526c4aa3f66acf676c574, + type: 3} + propertyPath: m_ApplyRootMotion + value: 0 + objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] @@ -2511,6 +2518,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: fe522b80fe58504408be0232c59db82f, type: 3} m_Name: m_EditorClassIdentifier: + playerLocomotion: {fileID: 668448702} animator: {fileID: 2098161784} canRotate: 1 --- !u!95 &2098161784 stripped 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); |
