using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControl : MonoBehaviour {
public float cameraSensitivity = 90;
public float climbSpeed = 4;
public float normalMoveSpeed = 10;
public float slowMoveSpeed = 0.25f;
public float fastMoveSpeed = 3;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start()
{
rotationX = transform.eulerAngles.y;
}
void Update () {
#region 控制摄像机的方向,鼠标箭头在哪,摄像机的方向就指向那
var dataTime = Time.deltaTime;
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * dataTime;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * dataTime;
rotationY = Mathf.Clamp(rotationY, -90, 90);
var tempRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
tempRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.localRotation = Quaternion.Slerp(transform.localRotation, tempRotation, dataTime * 6.0f);
#endregion
#region 利用方向键控制摄像机的移动
if(Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
transform.position += transform.forward * (normalMoveSpeed * fastMoveSpeed) * Input.GetAxis("Vertical") * dataTime;
transform.position += transform.right * (normalMoveSpeed * fastMoveSpeed) * Input.GetAxis("Horizontal") * dataTime;
}
else if(Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))
{
transform.position += transform.forward * (normalMoveSpeed * slowMoveSpeed) * Input.GetAxis("Vertical") * dataTime;
transform.position += transform.right * (normalMoveSpeed * slowMoveSpeed) * Input.GetAxis("Horizontal") * dataTime;
}
else
{
transform.position += transform.forward * normalMoveSpeed * Input.GetAxis("Vertical") * dataTime;
transform.position += transform.right * normalMoveSpeed * Input.GetAxis("Horizontal") * dataTime;
}
#endregion
#region 利用Q和E来控制摄像机的上下移动
if (Input.GetKey(KeyCode.Q))
{
transform.position -= transform.up * climbSpeed * dataTime;
}
if (Input.GetKey(KeyCode.E))
{
transform.position += transform.up * climbSpeed * dataTime;
}
#endregion
}
}