using UnityEngine;
using System.Collections;
using DG.Tweening;
using System;
/// <summary>
/// 摄像机旋转、缩放、平移完整版
/// </summary>
public class IBGMSMouseOrbitImproved : MonoBehaviour
{
public static IBGMSMouseOrbitImproved instance;
[Tooltip("初始目标")]
public Transform target;
[Tooltip("跟目标的距离")]
public float distance = 5.0f;
[Tooltip("X轴向的旋转速度")]
public float xrotSpeed = 0.1f;
[Tooltip("Y轴向的旋转速度")]
public float yrotSpeed = 10.0f;
[Tooltip("Y轴向的最小限制")]
public float yMinLimit = -10f;
[Tooltip("Y轴向的最大限制")]
public float yMaxLimit = 80f;
[Tooltip("摄像机和目标的最小距离")]
public float distanceMin = 0.5f;
[Tooltip("摄像机和目标的最大距离")]
public float distanceMax = 15f;
[Tooltip("摄像机旋转阻尼")]
public float smoothTime = 2f;
[Tooltip("摄像机平移X轴的速度")]
public float xMoveSpeed = 0.02f;
[Tooltip("摄像机平移Y轴的速度")]
public float yMoveSpeed = 0.02f;
[Tooltip("目标物的标签,只要点击此标签下的目标物,摄像机移动到目标位置附近")]
public string TargetTag;
public float rotationYAxis = 0.0f;
public float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
[Tooltip("摄像机滚轮的速度")]
public float speed = 5;
public bool isHdl = false;
private bool ismove = false;
private Transform nomalTar;
public float positionY = 0f;
/// <summary>
/// 最低点
/// </summary>
public float minNum = 100;
private void Awake()
{
instance = this;
}
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation =
true;
}
nomalTar = target;
Mid_Target = target.position;
}
Ray ray;
RaycastHit hit;
private Transform previousTarget;//上一个标记物
public bool isMove = false;
float time = 0;
[Tooltip("鼠标不动10秒后,此物体开始旋转")]
public GameObject allGame;
[Tooltip("此物体是否允许旋转")]
public bool isRotate = true;
private Vector3 v3in;
private Vector3 v3out;
private void Update()
{
rotationXAxis += positionY;
#region 效果参考海底捞项目.
//v3in = (transform.position - target.position).normalized * (transform.position - target.position).magnitude * 0.8f;
//v3out = (target.position - transform.position).normalized * (target.position - transform.position).magnitude * 2f;
//if (isHdl && !ismove)
//{
// ismove = true;
// isMove = true;
// transform.DOMove(transform.position - v3in, 1f);
// distance = Vector3.Distance(target.transform.position, transform.position - v3in);
// Invoke("MoveCamera", 1);
//}
//else if (!isHdl && ismove)
//{
// ismove = false;
// isMove = true;
// transform.DOMove(transform.position - v3out, 1f);
// distance = Vector3.Distance(target.transform.position, transform.position - v3out);
// Invoke("MoveCamera", 1);
//}
#endregion
//if (!Input.anyKey && Input.GetAxisRaw("Mouse X") == 0 && Input.GetAxisRaw("Mouse Y") == 0 && Input.GetAxis("Mouse ScrollWheel") == 0 && isRotate == true)
//{
// time += Time.deltaTime;
// if (time >= 3)
// {
// if (allGame)
// {
// //print("旋转");
// allGame.transform.RotateAround(target.position, new Vector3(0, 1, 0), 10f*Time.deltaTime);
// }
// }
//}
//else
//{
// time = 0;
//}
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(ray, out hit, 10000))
{
print(hit.transform.name);
if (hit.transform.tag == TargetTag)
{
isMove = true;
target = hit.transform;
Vector3 dis = Mid_Target - target.position;
Mid_Target = target.position;
Vector3 cameradis = transform.position - dis;
transform.DOMove(cameradis, 1f);
distance = Vector3.Distance(Mid_Target, cameradis);
Invoke("MoveCamera", 1f);
}
}
}
if (Input.GetMouseButton(2))
{
isMove = true;
Vector3 move = new Vector3(Input.GetAxis("Mouse X"), 0, Input.GetAxis("Mouse Y"));
Vector3 v3 = transform.up;
transform.position -= transform.right * move.x * distance * xMoveSpeed;
transform.position -= v3 * move.z * distance * yMoveSpeed;
Mid_Target -= transform.right * move.x * distance * xMoveSpeed;
Mid_Target -= v3 * move.z * distance * yMoveSpeed;
distance = Vector3.Distance(Mid_Target, transform.position);
}
if (Input.GetKey(KeyCode.Alpha2))
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
isMove = true;
Vector3 move = new Vector3(-Input.GetAxis("Horizontal"), 0, -Input.GetAxis("Vertical"));
Vector3 v3 = transform.up;
transform.position -= transform.right * move.x * distance * xMoveSpeed;
transform.position -= v3 * move.z * distance * yMoveSpeed;
Mid_Target -= transform.right * move.x * distance * xMoveSpeed;
Mid_Target -= v3 * move.z * distance * yMoveSpeed;
distance = Vector3.Distance(Mid_Target, transform.position);
}
}
if (Input.GetMouseButtonUp(2) || Input.GetKeyUp(KeyCode.Alpha2))
{
isMove = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
isMove = true;
target = nomalTar;
Mid_Target =target.position;
Vector3 cameradis = target.position ;
transform.DOMove(cameradis, 1f);
distance = 1f;
Invoke("MoveCamera", 1f);
}
}
[HideInInspector]
public Vector3 Mid_Target;
private void MoveCamera()
{
isMove = false;
}
void LateUpdate()
{
if (target)
{
if (!isMove)
{
if (Input.GetMouseButton(1))
{
velocityX += xrotSpeed * Input.GetAxis("Mouse X") * 0.02f;
velocityY += yrotSpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
if (Input.GetKey(KeyCode.Alpha1))
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
velocityX += xrotSpeed * -Input.GetAxis("Horizontal") * 0.02f;
velocityY += yrotSpeed * -Input.GetAxis("Vertical") * 0.02f;
}
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
if (Vector3.Distance(target.transform.position, transform.position) > distanceMax)
{
distanceMax = Vector3.Distance(target.transform.position, transform.position);
}
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * speed, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
if (hit.transform.name == target.name)
{
distance -= hit.distance;
}
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + Mid_Target;
transform.rotation = rotation;
transform.position = position;
//transform.position = new Vector3(position.x, Mathf.Clamp(position.y, minNum, 100000), position.z);
velocityX = Mathf.Lerp(velocityX, 0, Time.unscaledDeltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.unscaledDeltaTime * smoothTime);
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}
IBGMSMouseOrbitImproved(中键拖动改变焦点)
最新推荐文章于 2024-09-11 09:32:06 发布