using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
private Direction m_direc;
public Space m_RotateSpace;
public float m_RotateSpeed = 3;
public Camera cam;
//方向灵敏度
public float sensitivityX = 10F;
public float sensitivityY = 10F;
//上下最大视角(Y视角)
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
float rotationX = 0F;
private void Awake()
{
cam = transform.Find("Main Camera").GetComponent<Camera>();
Cursor.visible = false;
}
void Start () {
}
void Update () {
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * 3);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * Time.deltaTime * 3);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * 3);
}
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * 3);
}
if (Input.GetKey(KeyCode.LeftShift))
{
//Cursor.visible = false;
//根据鼠标移动的快慢(增量), 获得相机左右旋转的角度(处理X)
rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
//根据鼠标移动的快慢(增量), 获得相机上下旋转的角度(处理Y)
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
//角度限制. rotationY小于min,返回min. 大于max,返回max. 否则返回value
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
//旋转
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
transform.localEulerAngles = new Vector3(0,rotationX, 0);
}
}
}
unity简单实现第一视角移动和按住shift旋转
最新推荐文章于 2024-04-22 10:38:10 发布