```
feat(EnemyBaseMove): 添加重力系统和物理材质优化 - 添加重力加速度变量和垂直速度控制 - 实现applyGravity()和getVerticalVelocity()方法处理垂直运动 - 在Attack状态和静止时应用重力效果 - 为CharacterController添加新的物理材质引用 - 调整CharacterController中心位置从(0,0,0)到(0,1,0) - 在场景中禁用根动作应用以配合自定义移动系统 ```
This commit is contained in:
1 parent
cdae901b96
commit
b80d31101c
2 files changed
+50
-16
No files matched your search
@@ -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()
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user