Rigidbody r; ?? ?CharacterController a; ?? ?float speed = 100; ?? ?void Start () { ?? ??? ?//vertical英[?v??t?kl] horizontal 英 [?h?r??z?ntl] ?? ??? ?//1.角色控制器SimpleMove?? ?平面移动,自带碰撞器和重力 ?? ??? ?//Move空中飞行,没有重力,自带碰撞器 ?? ??? ?//a = GetComponent<CharacterController>(); ?? ??? ? ?? ??? ?//要改变一个物体的运动状态必须要对其施加力,Unity也为我们提供了AddForce()方法。 ?? ??? ?r = GetComponent<Rigidbody>();
?? ??? ?//2.Rigidbody 刚体 ?? ??? ?//Rigidbody.velocity
?? ?} ?? ?Vector3 dir; ?? ?// Update is called once per frame ?? ?void Update () { ?? ??? ?//Move(); ?? ??? ?float v = Input.GetAxis("Vertical") * Time.deltaTime * speed; ?? ??? ?float h = Input.GetAxis("Horizontal") * Time.deltaTime * speed; ?? ??? ?dir = new Vector3(h, 0, v)*speed*Time .deltaTime ?; ?? ??? ?r.velocity = dir;
?? ?} ?? ?void Move() ?? ?{ ?? ??? ?dir = Vector3.zero; ?? ??? ?//判断角色是否在地面isGrounded ?? ??? ?if (a.isGrounded) ?? ??? ?{ ?? ??? ??? ?float v = Input.GetAxis("Vertical") * Time.deltaTime * speed; ?? ??? ??? ?float h = Input.GetAxis("Horizontal") * Time.deltaTime * speed; ?? ??? ??? ?dir = new Vector3(h, 0, v); ?? ??? ??? ?if (Mathf.Abs(v) > 0 || Mathf.Abs(h) > 0) ?? ??? ??? ?{
?? ??? ??? ?} ?? ??? ?} ?? ??? ?a.SimpleMove(dir); ?? ?}
|