using UnityEngine;
using UnityEngine.UI;
using System.Collections;
// 将脚本挂载到摄像机上
public class CameraController : MonoBehaviour
{
//控制视野缩放的速率
public float view_value=100;
public float moveSpeed = 100; // 设置相机移动速度
public static Vector3 cameraPosition;
public static Quaternion cameraRotation;
public static GameObject MainCameraObj;
public static float cameraFieldofView;
public static GameObject shoolMapGameobjecct;
public static Vector3 schoolMapPosition;
public static Quaternion schoolMapRoation;
void Start()
{
MainCameraObj = this.gameObject;
cameraPosition = this.gameObject.transform.position;
cameraRotation = this.gameObject.transform.rotation;
cameraFieldofView = this.gameObject.GetComponent<Camera>().fieldOfView;
}
void Update()
{
if (SysConst.isCamera)
{
if (Input.GetAxis("Mouse ScrollWheel") != 0)
{
this.gameObject.transform.Translate(new Vector3(0, 0, Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * view_value));
}
// 当按住鼠标右键的时候
if (Input.GetMouseButton(0))
{
// 获取鼠标的x和y的值,乘以速度和Time.deltaTime是因为这个可以是运动起来更平滑
float h = Input.GetAxis("Mouse X") * moveSpeed * Time.deltaTime;
float v = Input.GetAxis("Mouse Y") * moveSpeed * Time.deltaTime;
// 设置当前摄像机移动,y轴并不改变
// 需要摄像机按照世界坐标移动,而不是按照它自身的坐标移动,所以加上Spance.World
this.transform.Translate(-h*1.5f, 0, -v*1.5f, Space.World);
}
//视角旋转
if (Input.GetMouseButton(1))
{
if (Mathf.Abs(Input.GetAxis("Mouse X"))>0||Mathf.Abs(Input.GetAxis("Mouse Y")) >0)
{
// this.transform.RotateAround(this.transform.position, Vector3.up, Input.GetAxis("Mouse X"));
if (Mathf.Abs(Input.GetAxis("Mouse X")) > Mathf.Abs(Input.GetAxis("Mouse Y")))
{
shoolMapGameobjecct.transform.RotateAround(shoolMapGameobjecct.transform.position, Vector3.up, Input.GetAxis("Mouse X"));
}
if (Mathf.Abs(Input.GetAxis("Mouse X")) < Mathf.Abs(Input.GetAxis("Mouse Y")))
{
shoolMapGameobjecct.transform.RotateAround(shoolMapGameobjecct.transform.position, Vector3.right, Input.GetAxis("Mouse Y"));
}
}
}
//视角还原
if (Input.GetKeyDown(KeyCode.Space))
{
//还原位置
MainCameraObj.transform.position = cameraPosition;
//还原旋转角度
MainCameraObj.transform.rotation = cameraRotation;
//还原摄像机广角
MainCameraObj.GetComponent<Camera>().fieldOfView = cameraFieldofView;
if (shoolMapGameobjecct != null)
{
shoolMapGameobjecct.transform.position = schoolMapPosition;
shoolMapGameobjecct.transform.rotation = schoolMapRoation;
}
}
}
}
}