public GameObject _cam;//相机
private int Cam_speed = 25;//相机速度
/// <summary>
/// 左键移动相机
/// </summary>
void OnGUI()
{
if (Event.current.type == EventType.MouseDrag && !Input.GetMouseButton(1))//Event.current.type事件,当前事件,类型 EventType.MouseDrag事件类型,鼠标拖拽
{
transform.Translate(Input.GetAxis("Mouse X") * Time.deltaTime * -60, 0, Input.GetAxis("Mouse Y") * Time.deltaTime * -60);
}
}
void Update()
{
XiangJiXuanZhuan();
XingJiXiDong();
GunLunSuoFang();
}
/// <summary>
/// 相机旋转
/// </summary>
public void XiangJiXuanZhuan()
{
if (Input.GetMouseButton(1))//右键
{
float mousX = Input.GetAxis("Mouse X");
this.transform.transform.Rotate(0, mousX, 0);
float mousY = Input.GetAxis("Mouse Y");
_cam.transform.Rotate(-mousY * Time.timeScale, 0, 0); // 旋转
if (_cam.transform.eulerAngles.x > 45 && _cam.transform.eulerAngles.x < 180)
{
_cam.transform.eulerAngles = new Vector3(45, _cam.transform.eulerAngles.y, _cam.transform.eulerAngles.z);
}
if (_cam.transform.eulerAngles.x < 330 && _cam.transform.eulerAngles.x > 180)
{
_cam.transform.eulerAngles = new Vector3(330, _cam.transform.eulerAngles.y, _cam.transform.eulerAngles.z);
}
}
}
/// <summary>
/// 相机移动(wasdqe)
/// </summary>
public void XingJiXiDong()
{
if (Input.GetKey("w"))
{
this.transform.Translate(Vector3.forward * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("a"))
{
this.transform.Translate(Vector3.left * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("d"))
{
this.transform.Translate(Vector3.right * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("s"))
{
this.transform.Translate(Vector3.back * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("e"))
{
this.transform.Translate(Vector3.down * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("q"))
{
this.transform.Translate(Vector3.up * Time.deltaTime * Cam_speed);
}
}
//滚轮缩放
public void GunLunSuoFang()
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
_cam.transform.Translate(0, 0, -300 * Time.deltaTime);
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
_cam.transform.Translate(0, 0, 300 * Time.deltaTime);
}
}
private int Cam_speed = 25;//相机速度
/// <summary>
/// 左键移动相机
/// </summary>
void OnGUI()
{
if (Event.current.type == EventType.MouseDrag && !Input.GetMouseButton(1))//Event.current.type事件,当前事件,类型 EventType.MouseDrag事件类型,鼠标拖拽
{
transform.Translate(Input.GetAxis("Mouse X") * Time.deltaTime * -60, 0, Input.GetAxis("Mouse Y") * Time.deltaTime * -60);
}
}
void Update()
{
XiangJiXuanZhuan();
XingJiXiDong();
GunLunSuoFang();
}
/// <summary>
/// 相机旋转
/// </summary>
public void XiangJiXuanZhuan()
{
if (Input.GetMouseButton(1))//右键
{
float mousX = Input.GetAxis("Mouse X");
this.transform.transform.Rotate(0, mousX, 0);
float mousY = Input.GetAxis("Mouse Y");
_cam.transform.Rotate(-mousY * Time.timeScale, 0, 0); // 旋转
if (_cam.transform.eulerAngles.x > 45 && _cam.transform.eulerAngles.x < 180)
{
_cam.transform.eulerAngles = new Vector3(45, _cam.transform.eulerAngles.y, _cam.transform.eulerAngles.z);
}
if (_cam.transform.eulerAngles.x < 330 && _cam.transform.eulerAngles.x > 180)
{
_cam.transform.eulerAngles = new Vector3(330, _cam.transform.eulerAngles.y, _cam.transform.eulerAngles.z);
}
}
}
/// <summary>
/// 相机移动(wasdqe)
/// </summary>
public void XingJiXiDong()
{
if (Input.GetKey("w"))
{
this.transform.Translate(Vector3.forward * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("a"))
{
this.transform.Translate(Vector3.left * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("d"))
{
this.transform.Translate(Vector3.right * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("s"))
{
this.transform.Translate(Vector3.back * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("e"))
{
this.transform.Translate(Vector3.down * Time.deltaTime * Cam_speed);
}
if (Input.GetKey("q"))
{
this.transform.Translate(Vector3.up * Time.deltaTime * Cam_speed);
}
}
//滚轮缩放
public void GunLunSuoFang()
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
_cam.transform.Translate(0, 0, -300 * Time.deltaTime);
}
else if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
_cam.transform.Translate(0, 0, 300 * Time.deltaTime);
}
}