diff --git a/Assets/Scenes/SampleScene.scene b/Assets/Scenes/SampleScene.scene index a0679c3..5bd085b 100644 --- a/Assets/Scenes/SampleScene.scene +++ b/Assets/Scenes/SampleScene.scene @@ -124,18 +124,6 @@ NavMeshSettings: debug: m_Flags: 0 m_NavMeshData: {fileID: 0} ---- !u!134 &14211372 -PhysicMaterial: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_Name: - dynamicFriction: 0.6 - staticFriction: 0.6 - bounciness: 0 - frictionCombine: 0 - bounceCombine: 0 --- !u!1 &46311543 GameObject: m_ObjectHideFlags: 0 @@ -1429,6 +1417,11 @@ PrefabInstance: value: objectReference: {fileID: 22100000, guid: c205ebfae56f644af94a7948cc0609be, type: 2} + - target: {fileID: 5866666021909216657, guid: aedf6e849dd924e229c57640f99ff50a, + type: 3} + propertyPath: m_ApplyRootMotion + value: 0 + objectReference: {fileID: 0} m_RemovedComponents: [] m_RemovedGameObjects: [] m_AddedGameObjects: [] @@ -1455,7 +1448,7 @@ CharacterController: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 989436104} - m_Material: {fileID: 14211372} + m_Material: {fileID: 1431018394} m_IncludeLayers: serializedVersion: 2 m_Bits: 0 @@ -1473,7 +1466,7 @@ CharacterController: m_StepOffset: 0.3 m_SkinWidth: 0.08 m_MinMoveDistance: 0.001 - m_Center: {x: 0, y: 0, z: 0} + m_Center: {x: 0, y: 1, z: 0} --- !u!114 &989436106 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1764,6 +1757,18 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1369034729} m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0} +--- !u!134 &1431018394 +PhysicMaterial: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: ' (Instance)' + dynamicFriction: 0.6 + staticFriction: 0.6 + bounciness: 0 + frictionCombine: 0 + bounceCombine: 0 --- !u!1 &1489219289 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Script/EnemyBaseMove.cs b/Assets/Script/EnemyBaseMove.cs index 4b7f067..ac31077 100644 --- a/Assets/Script/EnemyBaseMove.cs +++ b/Assets/Script/EnemyBaseMove.cs @@ -8,6 +8,9 @@ public class NewBehaviourScript : MonoBehaviour //移动速度 public float speed = 5f; + //重力加速度 + public float gravity = -20f; + //活动范围 public float range = 500f; @@ -35,6 +38,9 @@ public class NewBehaviourScript : MonoBehaviour // 角色控制器 private CharacterController controller; + //垂直速度 + private float verticalVelocity; + //状态枚举 public enum State { @@ -73,6 +79,7 @@ public class NewBehaviourScript : MonoBehaviour if (state == State.Attack) { animator.SetInteger("speed", 0); + applyGravity(); return; } @@ -164,19 +171,41 @@ public class NewBehaviourScript : MonoBehaviour if (movement == Vector3.zero) { animator.SetInteger("speed", 0); + applyGravity(); return; } //改变动画控制器的参数 animator.SetInteger("speed", 1); - //向目标点移动 - controller.Move(movement.normalized * Time.deltaTime * speed); + //向目标点移动,同时应用垂直重力 + Vector3 velocity = movement.normalized * speed; + velocity.y = getVerticalVelocity(); + controller.Move(velocity * Time.deltaTime); //改变角色朝向目标点,只改变y轴 transform.rotation = Quaternion.LookRotation(movement, Vector3.up); transform.rotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0); } + private float getVerticalVelocity() + { + if (controller.isGrounded && verticalVelocity < 0) + { + verticalVelocity = -2f; + } + else + { + verticalVelocity += gravity * Time.deltaTime; + } + + return verticalVelocity; + } + + private void applyGravity() + { + controller.Move(Vector3.up * getVerticalVelocity() * Time.deltaTime); + } + //随机目标点 private void randomTargetPos() {