废话没有,直接上代码:
1 using UnityEngine; 2 using System.Collections; 3 4 public class SuperWalk : MonoBehaviour { 5 6 public float speed = 10.0f;//移动速度 7 public float jumpSpeed = 8.0f;//跳跃速度 8 public float gravity = 20.0f;//重力 9 10 private Vector3 moveDirection = Vector3.zero;//移动方向 11 private bool grounded = false;//是否脚下有物体 12 13 CharacterController controller;//角色控制器 14 15 void FixedUpdate () { 16 if (grounded) {//如果在地面 17 //获取移动方向 18 moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical")); 19 moveDirection = transform.TransformDirection(moveDirection); 20 moveDirection *= speed; 21 22 //如果按了跳跃按钮 23 if (Input.GetButton("Jump")) { 24 moveDirection.y = jumpSpeed; 25 } 26 } 27 moveDirection.y -= gravity * Time.deltaTime; 28 //获取角色控制器 29 controller = GetComponent<CharacterController>(); 30 //获取角色移动返回的位掩码 31 CollisionFlags v = controller.Move(moveDirection * Time.deltaTime); 32 33 //是否脚下有物体(个人理解) 34 grounded = (v & CollisionFlags.CollidedBelow) != 0; 35 36 } 37 }
请大神们指点
来源:吃豆游戏作者