让camera实现类似cs第一人称视角旋转和位移

直接把这个脚本挂在摄像机上就可:

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

/*
 *  键盘鼠标位移和cs操作模式完全一致
 *  wsad四键 控制camera x和z轴方向位移
 *  鼠标 控制camera角度旋转
 *  
 *  camera位移时受当时camera角度的影响
 */

public class MainCameraScript : MonoBehaviour {

    private float speed = 8.0f;
    private float rotationSpeed = 4.0f;
    private bool isKeyDown_W = false;
    private bool isKeyDown_S = false;
    private bool isKeyDown_A = false;
    private bool isKeyDown_D = false;
    //private bool isKeyDown_Space = false;


    //x轴旋转方向是反的,所以不能直接用当前欧拉角x去加
    private float curRotationX = 0.0f;

    // Use this for initialization
    void Start () {
        float posX = gameObject.transform.position.x;
        float posZ = gameObject.transform.position.z;
        float posY = gameObject.transform.position.y;
        Debug.Log("maincamera pos x is:" + posY);
        Vector3 rotation = transform.eulerAngles;
        Debug.Log("maincamera eulerAngles y is : " + rotation.y);


        Cursor.visible = false;
    }
	
	// Update is called once per frame
	void Update () {
        mouseInput();
        keyboardInput();

    }

    private void mouseInput()
    {
        curRotationX = curRotationX + rotationSpeed * Input.GetAxis("Mouse Y");
        float rotationY = transform.localEulerAngles.y + rotationSpeed * Input.GetAxis("Mouse X");
        transform.localEulerAngles = new Vector3(-curRotationX, rotationY,0);


        Debug.Log("maincamera eulerAngles y is : " + transform.localEulerAngles.y);
    }

    private void keyboardInput()
    {
        //Input.GetAxis 按键发生时的增量 最小-1最大1

        //旋转
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime;
        //transform.Rotate(0, rotation, 0);

        //欧拉角 unity右侧inspector的transform的Rotation里显示的就是欧拉角数值
        float eulerAnglesY = transform.eulerAngles.y;
        //Debug.Log("eulerAnglesY  is : " + eulerAnglesY.ToString());
        //前进后退,旋转后会影响到前进后退的方向
        if (Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("w key down");
            isKeyDown_W = true;
            //DispatchKeyboardEvent(KeyCode.W, isKeyDown_W);
        }
        else if (Input.GetKeyUp(KeyCode.W))
        {
            isKeyDown_W = false;
            //DispatchKeyboardEvent(KeyCode.W, isKeyDown_W);
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            Debug.Log("s key down");
            isKeyDown_S = true;
            //DispatchKeyboardEvent(KeyCode.S, isKeyDown_S);
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            isKeyDown_S = false;
            //DispatchKeyboardEvent(KeyCode.S, isKeyDown_S);
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            isKeyDown_A = true;
            //DispatchKeyboardEvent(KeyCode.A, isKeyDown_A);

        }
        else if (Input.GetKeyUp(KeyCode.A))
        {
            isKeyDown_A = false;
            //DispatchKeyboardEvent(KeyCode.A, isKeyDown_A);
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            isKeyDown_D = true;
            //DispatchKeyboardEvent(KeyCode.D, isKeyDown_D);
        }
        else if (Input.GetKeyUp(KeyCode.D))
        {
            isKeyDown_D = false;
            //DispatchKeyboardEvent(KeyCode.D, isKeyDown_D);
        }

        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //    isKeyDown_Space = true;
        //}
        //else if (Input.GetKeyUp(KeyCode.Space))
        //{
        //    isKeyDown_Space = false;
        //}

        if (isKeyDown_W)
        {
            float dx = Mathf.Sin(eulerAnglesY * Mathf.Deg2Rad) * speed * Time.deltaTime;
            float dz = Mathf.Cos(eulerAnglesY * Mathf.Deg2Rad) * speed * Time.deltaTime;
            transform.position += new Vector3(dx, 0, dz);
        }

        if (isKeyDown_S)
        {
            float dx = Mathf.Sin(eulerAnglesY * Mathf.Deg2Rad) * -speed * Time.deltaTime;
            float dz = Mathf.Cos(eulerAnglesY * Mathf.Deg2Rad) * -speed * Time.deltaTime;
            transform.position += new Vector3(dx, 0, dz);
        }

        if(isKeyDown_A)
        {

            //eulerAnglesY不能直接用
            float dx = Mathf.Cos(0-eulerAnglesY * Mathf.Deg2Rad) * -speed * Time.deltaTime;
            float dz = Mathf.Sin(0-eulerAnglesY * Mathf.Deg2Rad) * -speed * Time.deltaTime;
            transform.position += new Vector3(dx, 0, dz);
        }

        if (isKeyDown_D)
        {
            float dx = Mathf.Cos(0-eulerAnglesY * Mathf.Deg2Rad) * speed * Time.deltaTime;
            float dz = Mathf.Sin(0-eulerAnglesY * Mathf.Deg2Rad) * speed * Time.deltaTime;
            transform.position += new Vector3(dx, 0, dz);
        }

        //if (isKeyDown_Space)
        //{
        //    float dy = speed * Time.deltaTime;
        //    transform.position += new Vector3(0, dy, 0);
        //}


    }
}

  

原文地址:https://www.cnblogs.com/JD85/p/6531647.html