Unity 角色移动

添加一个平面和一个胶囊作为地面和角色,胶囊添加Character Controller组件

 脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerComtrol : MonoBehaviour
{
    private CharacterController player;
    // Start is called before the first frame update
    void Start()
    {
        player = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        //水平轴 (PS:虚拟轴的时候讲过)
        float horizontal = Input.GetAxis("Horizontal");
        //垂直轴
        float vertical = Input.GetAxis("Vertical");
        //创建成一个方向向量
        Vector3 dir = new Vector3(horizontal, 0, vertical);
        // Debug.DrawRay(transform.position, dir, Color.red);
        //朝向该方向移动
        // player.SimpleMove(dir);
        //如果按住Shift键就加速
        if (Input.GetKey(KeyCode.LeftShift))
        {
            player.SimpleMove(dir * 3);
        }else{
            player.SimpleMove(dir);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/ssl267422/article/details/128823525