153 lines
4.7 KiB
C#
153 lines
4.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using DG.Tweening;
|
|
public class CameraOrbit : MonoBehaviour
|
|
{
|
|
public Transform target;
|
|
public float distance = 5f;
|
|
public float xSpeed = 120f;
|
|
public float ySpeed = 120f;
|
|
public float scrollSpeed = 1f;
|
|
public float yMinLimit = -20f;
|
|
public float yMaxLimit = 80f;
|
|
public float distanceMin = 0.5f;
|
|
public float distanceMax = 15f;
|
|
public float smoothTime = 2f;
|
|
public float rotationYAxis;
|
|
public float rotationXAxis;
|
|
public float velocityX;
|
|
public float velocityY;
|
|
private bool start = true;
|
|
public static bool isMove = true;
|
|
private Transform pos;//获取Empty的子物体pos
|
|
private Vector3 offset;//获取初始时镜头相对于pos的偏移量
|
|
private Transform mainCamera;
|
|
public bool limit = true;
|
|
private bool Enable = true;
|
|
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);
|
|
}
|
|
|
|
private void LateUpdate()
|
|
{
|
|
|
|
if (Enable && this.target != null && isMove)
|
|
{
|
|
//FirstPersonController firstPersonController = GameObject.FindGameObjectWithTag("Player").GetComponent<FirstPersonController>();
|
|
//firstPersonController.playerCanMove = Input.GetMouseButton(1);
|
|
if (Input.GetMouseButton(1))
|
|
{
|
|
this.start = false;
|
|
if (limit)
|
|
{
|
|
|
|
}
|
|
else
|
|
{
|
|
this.velocityX += (this.xSpeed * Input.GetAxis("Mouse X")) * 0.02f;
|
|
this.velocityY += (this.ySpeed * Input.GetAxis("Mouse Y")) * 0.02f;
|
|
}
|
|
}
|
|
//if (!this.start)
|
|
//{
|
|
this.rotationYAxis += this.velocityX;
|
|
this.rotationXAxis -= this.velocityY;
|
|
this.rotationXAxis = ClampAngle(this.rotationXAxis, this.yMinLimit, this.yMaxLimit);
|
|
Quaternion quaternion2 = Quaternion.Euler(this.rotationXAxis, this.rotationYAxis, 0f);
|
|
Vector3 vector = new Vector3(0f, 0f, -this.distance);
|
|
Vector3 vector2 = ((Vector3)(quaternion2 * vector)) + this.target.position;
|
|
base.transform.rotation = quaternion2;
|
|
base.transform.position = vector2;
|
|
this.velocityX = Mathf.Lerp(this.velocityX, 0f, Time.deltaTime * this.smoothTime);
|
|
this.velocityY = Mathf.Lerp(this.velocityY, 0f, Time.deltaTime * this.smoothTime);
|
|
|
|
float axis = 0;
|
|
if (limit)
|
|
{ }
|
|
else
|
|
{
|
|
axis = Input.GetAxis("Mouse ScrollWheel");
|
|
}
|
|
if ((axis < 0f) && (this.distance < this.distanceMax))
|
|
{
|
|
this.distance += this.scrollSpeed;
|
|
}
|
|
else if ((axis > 0f) && (this.distance > this.distanceMin))
|
|
{
|
|
this.distance -= this.scrollSpeed;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Resume()
|
|
{
|
|
Enable = false;
|
|
|
|
|
|
|
|
transform.DOLocalMove(initPos, 1).OnComplete(() =>
|
|
{
|
|
Vector3 eulerAngles = base.transform.eulerAngles;
|
|
this.rotationYAxis = eulerAngles.y;
|
|
this.rotationXAxis = eulerAngles.x;
|
|
velocityX = 0;
|
|
distance = initDis;
|
|
velocityY = 0;
|
|
Enable = true;
|
|
});
|
|
transform.DOLocalRotate(initRot, 1);
|
|
}
|
|
public Vector3 initPos, initRot; float initDis;
|
|
private void Start()
|
|
{
|
|
Vector3 eulerAngles = base.transform.eulerAngles;
|
|
this.rotationYAxis = eulerAngles.y;
|
|
this.rotationXAxis = eulerAngles.x;
|
|
//SliderValue(this.distance);
|
|
|
|
StartCoroutine(Delay());
|
|
}
|
|
IEnumerator Delay()
|
|
{
|
|
yield return 1;
|
|
initDis = this.distance;
|
|
initPos = transform.localPosition;
|
|
initRot = transform.localEulerAngles;
|
|
}
|
|
public bool InScene()
|
|
{
|
|
bool inscene = false;
|
|
if (Input.mousePosition.x > 0 && Input.mousePosition.x < Screen.width && Input.mousePosition.y > 0 && Input.mousePosition.y < Screen.height)
|
|
inscene = true;
|
|
else
|
|
inscene = false;
|
|
return inscene;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 相机穿墙
|
|
/// </summary>
|
|
private void AvoidCrossWall()
|
|
{
|
|
RaycastHit hit;
|
|
if (Physics.Raycast(pos.position, mainCamera.position - pos.position, out hit, offset.magnitude))
|
|
{
|
|
Debug.DrawLine(pos.position, hit.point);
|
|
if (hit.collider.tag == "Wall")
|
|
{
|
|
mainCamera.position = hit.point;
|
|
}
|
|
}
|
|
}
|
|
}
|