内容纲要

场景搭建CharMove插图

给地面添加材质并选择网格
给胶囊体上的cube添加材质

角色行走

public class PlayerCtrl : MonoBehaviour
{
    public float moveSpeed = 5f;
    public float rotSpeed = 180f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");//左右键
        float v = Input.GetAxis("Vertical");//上下键
        float mouseX= Input.GetAxis("Mouse X");
        if (v < 0)//向后移动速度减慢
        {
            v *= 0.5f;  
        }
        transform.Translate(0, 0, v*moveSpeed*Time.deltaTime, Space.Self);//局部坐标系移动
        if (Input.GetMouseButton(1))
        {

            this.transform.Rotate(0, mouseX * rotSpeed * Time.deltaTime, 0, Space.Self);
        }
        else
        {
            this.transform.Rotate(0,h*rotSpeed*Time.deltaTime, 0, Space.Self);

        }

    }
}

发表评论