Unity Rigidbody第一人称移动(按键移动鼠标控制朝向)

1、将地面添加盒体碰撞器(BoxCollider),周围物体防止穿模用盒体或胶囊体碰撞器(Capsule Collider)简单拼接出物体的形状即可。
2、首先在控制器上添加胶囊体碰撞器(Capsule Collider),与盒体碰撞器(Box Collidr)的区别是移动更加平滑。再添加刚体(Rigidbody),刚体的约束(Constraints)设置中将位置解冻(FreezePostion)和旋转解冻(FreezeRotation)都✔设置为True.
3、在控制器上添加脚本控制,即可。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerMove : MonoBehaviour
{
    
    
    public  float moveSpeed;//摄像机的移动速度
    public GameObject Eye;
    private Vector3 m_camRot;
    public float m_rotateSpeed = 1;//旋转系数

    Vector3 rot = new Vector3(0, 0, 0);

    void Update()
    {
    
    
        //鼠键控制移动
        WASD();

        if (Input.anyKey)
        {
    
    
            //旋转做解冻,位置保持冻结
            this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
        }
        else
        {
    
    
           //位置和旋转都解冻,即位置和旋转不做限制都可动
            this.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
        }
    }

    /// <summary>
    /// 鼠键控制player移动
    /// </summary>
    void WASD()
    {
    
    
        if (Input.GetMouseButton(0))
        {
    
    
            if (Input.GetAxis("Mouse X") != 0)
            {
    
    
                //Debug.Log(Input.GetAxis("Mouse X"));
                if (Input.GetAxis("Mouse X") < 0.1f && Input.GetAxis("Mouse X") > -0.1f)
                {
    
    
                    // return;
                }
                this.gameObject.transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * Time.fixedDeltaTime * 100, 0));//摄像机的旋转速度

                //clearArrow(false);
            }
            if (Input.GetAxis("Mouse Y") != 0)
            {
    
    
                if (Input.GetAxis("Mouse Y") < 0.1f && Input.GetAxis("Mouse Y") > -0.1f)
                {
    
    
                    //Debug.Log("返回");
                    //  return;
                }
                Eye.transform.Rotate(new Vector3(Input.GetAxis("Mouse Y") * Time.fixedDeltaTime * -100, 0, 0));//摄像机的旋转速度
            }
            //获取鼠标移动距离
            //float rh = Input.GetAxis("Mouse X");
            //float rv = Input.GetAxis("Mouse Y");
             旋转摄像机
            //m_camRot.x -= rv * m_rotateSpeed;
            //m_camRot.y += rh * m_rotateSpeed;

        }
        //Eye.transform.localPosition = Vector3.zero;
        //Eye.transform.localEulerAngles = m_camRot;


        if (Input.GetKey(KeyCode.W))
        {
    
    
            //按自身朝向的前向移动
            gameObject.transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
        }
        if (Input.GetKey(KeyCode.S))
        {
    
    
            gameObject.transform.Translate(-Vector3.forward * Time.deltaTime * moveSpeed);
        }

        if (Input.GetKey(KeyCode.A))
        {
    
    
            gameObject.transform.Translate(-Vector3.right * Time.deltaTime * moveSpeed);
        }
        if (Input.GetKey(KeyCode.D))
        {
    
    
            gameObject.transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_22975451/article/details/114221459