using UnityEngine;
public class CameraController : MonoBehaviour
{
//前后左右:WASD,基于世界坐标
//上下:鼠标滚轮
//旋转:鼠标点击拖拽
public float speed = 10000;//键盘操控移动速度
public float mouseSpeed = 5000;//鼠标滚轮移动速度
private float xRotation;
private float yRotation;
public int yRotationMinLimit = -20;//控制旋转范围
public int yRotationMaxLimit = 80;
private float xRotationSpeed = 80;//控制旋转速度
private float yRotationSpeed = 80;
private Camera mainCamera;
private void Start()
{
mainCamera = GetComponent<Camera>();
}
void Update()
{
CameraRotation();//旋转模块
CameraMove();//移动模块:WASD、滚轮
}
void CameraRotation()
{
float MouseX = Input.GetAxis("Mouse X");
float MouseY = Input.GetAxis("Mouse Y");
if (Input.GetMouseButton(0))
{
xRotation -= MouseX * xRotationSpeed * 0.02f;
yRotation += MouseY * yRotationSpeed * 0.02f;
yRotation = ClampValue(yRotation, yRotationMinLimit, yRotationMaxLimit);
Quaternion rotation = Quaternion.Euler(-yRotation, -xRotation, 0);
transform.rotation = rotation;
}
}
float ClampValue(float value, float min, float max)//控制旋转的角度
{
if (value < -360)
value += 360;
if (value > 360)
value -= 360;
return Mathf.Clamp(value, min, max);//限制value的值在min和max之间, 如果value小于min,返回min。 如果value大于max,返回max,否则返回value
}
public void CameraMove()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float mouse = Input.GetAxis("Mouse ScrollWheel");
transform.Translate(new Vector3(h * speed * 3, mouse * mouseSpeed * 50, v * speed * 3) * Time.deltaTime, Space.World);
}
}
【Unity】Unity3D控制Camera移动
猜你喜欢
转载自blog.csdn.net/simplenthpower/article/details/128670616
今日推荐
周排行