首页 > 代码库 > CharacterController移动角色(SimpleMove)

CharacterController移动角色(SimpleMove)

SimpleMove

Y 轴上速度被忽略。速度以米/秒为单位。重力被自动应用。返回如果角色着地。建议你每帧只调用一次Move或者SimpleMove。

using UnityEngine;using System.Collections;public class Test1 : MonoBehaviour {    //声明一个CharacterController类型的私有变量    private CharacterController controller;    //角色的初始移动速度    private float playerSpeed=0.0f;    void Start ()     {        //获取CharacterController组件        controller=GetComponent<CharacterController>();    }        void Update ()     {        //角色的朝向        Vector3 playerForward=this.transform.TransformDirection(Vector3.forward);        //向前移动        if (Input.GetKey (KeyCode.W))        {            playerSpeed=5.0f;        }        else if (Input.GetKey(KeyCode.S))        {            playerSpeed=-5.0f;        }        else if (Input.GetKey(KeyCode.A))        {            this.transform.Rotate (Vector3.up*-10);        }        else if (Input.GetKey(KeyCode.D))        {            this.transform.Rotate (Vector3.up*10);        }        else        {            playerSpeed=0.0f;        }        controller.SimpleMove(playerForward*playerSpeed);    }}

 

CharacterController移动角色(SimpleMove)