307 lines
8.6 KiB
C#
307 lines
8.6 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
|
|
public class CameraView : MonoBehaviour
|
|
{
|
|
public enum RotationAxes
|
|
{
|
|
MouseXAndY = 0,
|
|
MouseX = 1,
|
|
MouseY = 2
|
|
}
|
|
public RotationAxes axes = RotationAxes.MouseXAndY;
|
|
public float sensitivityHor = 1f;
|
|
public float sensitivityVert = 1f;
|
|
|
|
public float moveSpeed = 5f;
|
|
|
|
public float minmumVert = -89f;
|
|
public float maxmumVert = 89f;
|
|
|
|
private float _rotationX = 0;
|
|
private float _rotationY = 0;
|
|
|
|
private Vector3 initPos;
|
|
private Quaternion initRotation;
|
|
|
|
//回到初始视角
|
|
private bool isBack;
|
|
//移动到下一个视角
|
|
private bool isMove;
|
|
//是否需要移动到下一个视角
|
|
private bool isCanMove;
|
|
|
|
|
|
private Vector3 nextPos;
|
|
private Quaternion nextAngle;
|
|
|
|
//相机是否在房间内
|
|
private bool isNear;
|
|
|
|
/// <summary>
|
|
/// 点击UI回到初始视角
|
|
/// </summary>
|
|
public bool isUIInit;
|
|
|
|
|
|
// Use this for initialization
|
|
void Start()
|
|
{
|
|
//Cursor.lockState = CursorLockMode.Locked;//锁定指针到视图中心
|
|
//Cursor.visible = false;//隐藏指针
|
|
_rotationX = transform.localEulerAngles.x;
|
|
initPos = transform.position;
|
|
initRotation = transform.rotation;
|
|
|
|
|
|
|
|
}
|
|
/// <summary>
|
|
/// 回到初始视角(无动画)
|
|
/// </summary>
|
|
public void InitView() {
|
|
|
|
transform.position = initPos;
|
|
transform.rotation = initRotation;
|
|
_rotationX = transform.localEulerAngles.x;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 动画移动到初始视角
|
|
/// </summary>
|
|
public void InitViewAnim() {
|
|
//停止上一次动画
|
|
StopCamMove();
|
|
isBack = true;
|
|
isCanMove = false;
|
|
isMove = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移动相机:先回到初始视角,再移动到目标视角
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <param name="angle"></param>
|
|
public void CamerMoveAnimAfter(Vector3 pos, Quaternion angle) {
|
|
//停止上一次动画
|
|
StopCamMove();
|
|
isBack = true;
|
|
isCanMove = true;
|
|
nextAngle = angle;
|
|
nextPos = pos;
|
|
}
|
|
/// <summary>
|
|
/// 移动相机:相机在房间内,先回到初始角度再移至目标位置,相机在房间外,直接移动到目标位置
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <param name="angle"></param>
|
|
public void CamerMoveAnim(Vector3 pos, Quaternion angle)
|
|
{
|
|
if (isNear)
|
|
CamerMoveAnimAfter(pos,angle);
|
|
else
|
|
CameraMoveTarget(pos, angle);
|
|
|
|
|
|
}
|
|
/// <summary>
|
|
/// 移动相机:直接动画移动到目标位置
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <param name="angle"></param>
|
|
public void CameraMoveTarget(Vector3 pos, Quaternion angle) {
|
|
StopCamMove();
|
|
isBack = false;
|
|
isCanMove = false;
|
|
nextAngle = angle;
|
|
nextPos = pos;
|
|
isMove = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 停止相机移动动画
|
|
/// </summary>
|
|
public void StopCamMove()
|
|
{
|
|
if (isBack || isMove)
|
|
{
|
|
_rotationX = RotationX(transform.localEulerAngles.x);
|
|
_rotationY = transform.localEulerAngles.y;
|
|
}
|
|
isBack = false;
|
|
isMove = false;
|
|
isCanMove = false;
|
|
isUIInit = false;
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
|
|
//动画移动相机回到初始视角
|
|
if (isBack)
|
|
{
|
|
transform.position = Vector3.Lerp(transform.position, initPos, Time.deltaTime);
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, initRotation, Time.deltaTime);
|
|
if (isBack && Vector3.Distance(transform.position, initPos) <=1f&& Vector3.Distance(transform.eulerAngles, initRotation.eulerAngles) <=1f)
|
|
{
|
|
if (isCanMove)
|
|
{
|
|
isCanMove=false;
|
|
//开始移动到下一视角
|
|
isMove = true;
|
|
Debug.Log("移动到下一视角");
|
|
}
|
|
if (isUIInit)
|
|
{
|
|
isUIInit=false;
|
|
Main.intance.m_htmlPanel.CallFunction("toggleMenuActive");
|
|
}
|
|
|
|
isBack = false;
|
|
//同步当前俯仰角度
|
|
_rotationX = RotationX(transform.localEulerAngles.x);
|
|
_rotationY = transform.localEulerAngles.y;
|
|
|
|
}
|
|
|
|
}
|
|
//动画移动相机
|
|
if (isMove)
|
|
{
|
|
transform.position = Vector3.Lerp(transform.position, nextPos, Time.deltaTime);
|
|
transform.rotation = Quaternion.Lerp(transform.rotation, nextAngle, Time.deltaTime);
|
|
if (isMove && Vector3.Distance(transform.position, nextPos) <= 1f && Vector3.Distance(transform.eulerAngles, nextAngle.eulerAngles) <= 1f)
|
|
{
|
|
isMove = false;
|
|
Debug.Log("移动结束");
|
|
//同步当前俯仰角度
|
|
_rotationX = RotationX(transform.localEulerAngles.x);
|
|
_rotationY = transform.localEulerAngles.y;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (Input.GetKeyDown(KeyCode.F))
|
|
{
|
|
//InitView();
|
|
isBack = true;
|
|
//CamerMoveAnim(Vector3.zero,Main.intance.transform.rotation);
|
|
}
|
|
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
//当自主操作时,停止动画移动
|
|
StopCamMove();
|
|
|
|
if (axes == RotationAxes.MouseX)
|
|
{
|
|
_rotationY+= Input.GetAxis("Mouse X") * sensitivityHor;
|
|
transform.rotation = Quaternion.Euler(_rotationX, _rotationY,0);
|
|
//transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityHor, 0);
|
|
//transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y - Input.GetAxis("Mouse X") * sensitivityHor, 0);
|
|
}
|
|
else if (axes == RotationAxes.MouseY)
|
|
{
|
|
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
|
|
_rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);
|
|
//float rotationY = transform.localEulerAngles.y;
|
|
transform.rotation = Quaternion.Euler(_rotationX, _rotationY, 0);
|
|
//transform.localEulerAngles = new Vector3(_rotationX, _rotationY, 0);
|
|
}
|
|
else
|
|
{
|
|
_rotationX -= Input.GetAxis("Mouse Y") * sensitivityVert;
|
|
_rotationX = Mathf.Clamp(_rotationX, minmumVert, maxmumVert);
|
|
float delta = Input.GetAxis("Mouse X") * sensitivityHor;
|
|
_rotationY +=delta;
|
|
transform.rotation = Quaternion.Euler(_rotationX, _rotationY, 0);
|
|
//transform.localEulerAngles = new Vector3(_rotationX, _rotationY, 0);
|
|
}
|
|
|
|
if (Input.GetKey(KeyCode.W))
|
|
{
|
|
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed);
|
|
}
|
|
if (Input.GetKey(KeyCode.S))
|
|
{
|
|
transform.Translate(Vector3.back * Time.deltaTime * moveSpeed);
|
|
}
|
|
if (Input.GetKey(KeyCode.A))
|
|
{
|
|
transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
|
|
}
|
|
if (Input.GetKey(KeyCode.D))
|
|
{
|
|
transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
|
|
}
|
|
if (Input.GetKey(KeyCode.E))
|
|
{
|
|
transform.Translate(Vector3.up * Time.deltaTime * moveSpeed);
|
|
}
|
|
if (Input.GetKey(KeyCode.Q))
|
|
{
|
|
transform.Translate(Vector3.down * Time.deltaTime * moveSpeed);
|
|
}
|
|
}
|
|
|
|
//判断相机是否进入房间内
|
|
|
|
if (transform.position.y > 3)
|
|
{
|
|
isNear = false;
|
|
//显示空间Icon
|
|
ModelManager.instance.CameraNear(false);
|
|
}
|
|
else
|
|
{
|
|
isNear = true;
|
|
//隐藏空间Icon
|
|
ModelManager.instance.CameraNear(true);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//private void OnTriggerEnter(Collider other)
|
|
//{
|
|
// Debug.Log("相机进入");
|
|
// if (other.transform.tag == "RoomSpace")
|
|
// {
|
|
// //隐藏空间Icon
|
|
// ModelManager.instance.ShowSpaceIcon(false);
|
|
// }
|
|
|
|
//}
|
|
|
|
//private void OnTriggerExit(Collider other)
|
|
//{
|
|
|
|
// Debug.Log("相机退出");
|
|
// if (other.transform.tag == "RoomSpace")
|
|
// {
|
|
// //显示空间Icon
|
|
// ModelManager.instance.ShowSpaceIcon(true);
|
|
// }
|
|
//}
|
|
|
|
private float RotationX(float x)
|
|
{
|
|
if (x > maxmumVert)
|
|
{
|
|
//当前值应该为负数
|
|
return x - 360;
|
|
}
|
|
return x;
|
|
|
|
}
|
|
|
|
}
|
|
|