Unity first person perspective walking and jumping

Add an empty game object as the first-person player, add character controllers, rigid body components, and bind script script
player settings
code

public class Player : MonoBehaviour
{
    
    
    /// <summary>
    /// 摄像机Transform
    /// </summary>
    Transform m_camTransform;
    /// <summary>
    /// 摄像机旋转角度
    /// </summary>
    Vector3 m_camRot;
    /// <summary>
    /// 摄像机高度
    /// </summary>
    float m_camHeight = 1.4f;

    public Transform m_transform;
    /// <summary>
    /// 角色控制器组件
    /// </summary>
    CharacterController m_ch;
    /// <summary>
    /// 角色移动速度
    /// </summary>
    float m_movSpeed = 3.0f;
    /// <summary>
    /// 重力
    /// </summary>
    float m_gravity = 2.0f;

    Rigidbody m_rg;
    float m_jumpGravity = 500f;

    // Start is called before the first frame update
    void Start()
    {
    
    
        m_transform = this.transform;
        //获取角色控制器组件
        m_ch = this.GetComponent<CharacterController>();

        m_rg = this.GetComponent<Rigidbody>();
        //获取摄像机
        m_camTransform = Camera.main.transform;
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
        //设置摄像机的旋转方向与主角一致
        m_camTransform.rotation = m_transform.rotation;
        m_camRot = m_camTransform.eulerAngles;
        //锁定鼠标
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        Control();
    }

    // 跳起来的速度
    public float jumpSpeed = 10.0f;
    // 重力
    public float gravity = -9.8f;
    // 最终垂直速度
    public float endsVelocity = -10.0f;
    // 在地面时的垂直速度
    public float minFall = -1.5f;
    // 垂直速度
    private float verSpeed;
    void Control()
    {
    
    
        Vector3 movement;
        //定义3个值控制移动
        float xm = 0, ym = 0, zm = 0;
        重力运动
        //ym -= m_gravity * Time.deltaTime;
        //前后左右移动
        if (Input.GetKey(KeyCode.W))
        {
    
    
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))
        {
    
    
            zm -= m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.A))
        {
    
    
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D))
        {
    
    
            xm += m_movSpeed * Time.deltaTime;
        }
        //使用角色控制器提供的Move函数进行移动
        //m_ch.Move(m_transform.TransformDirection(new Vector3(xm, ym, zm)));
        movement = m_transform.TransformDirection(new Vector3(xm, ym, zm));

        //跳跃

        // 角色控件自带的一个方法,用于检测是否在地面
        if (m_ch.isGrounded)
        {
    
    
            // 按了空格键则给垂直方向施加一个速度
            if (Input.GetButtonDown("Jump"))
            {
    
    
                verSpeed = jumpSpeed;
            }
            else
            {
    
    
                verSpeed = minFall;
            }
        }
        else
        {
    
    
            // 若已经跳起来了则将垂直方向的速度递减降低,来达到一个 下上下 的一个效果
            // Time.deltaTime 表示为每秒的刷新频率的倒数,用来控制每台电脑的移动速度都是一样的
            verSpeed += gravity * 3 * Time.deltaTime;
            // 限制最大坠落速度
            if (verSpeed < endsVelocity)
            {
    
    
                verSpeed = endsVelocity;
            }
        }
        // 给移动一个垂直速度
        movement.y = verSpeed * Time.deltaTime;

        // 控制速度
        //movement *= Time.deltaTime;
        // 角色控件自带的一个方法,若用 transform.Translate() 的话会无视碰撞器
        m_ch.Move(movement);

        //获取鼠标移动距离
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");
        //旋转摄像机
        m_camRot.x -= rv;
        m_camRot.y += rh;
        m_camTransform.eulerAngles = m_camRot;
        //使角色的面向方向与摄像机一致
        Vector3 camrot = m_camTransform.eulerAngles;
        camrot.x = 0; camrot.z = 0;
        m_transform.eulerAngles = camrot;
        //操作角色移动
        //使摄像机位置与角色一致
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
    }
}

Effect

Guess you like

Origin blog.csdn.net/qq_29242649/article/details/112325267