162 lines
5.6 KiB
C#
162 lines
5.6 KiB
C#
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
|
|
public class CameraControl : MonoBehaviour
|
|
{
|
|
public static CameraControl Instance { private set; get; }
|
|
public float minDis = 0.5f;
|
|
public float maxDis = 1.5f;
|
|
public float rSmoothTime = 0.3f, sSmoothTime = 0.3f, mSmoothTime = 0.3f;
|
|
public float mouseRotSpeed = 8, mouseMoveSpeed = 1, mouseZoomSpeed = 15;
|
|
public float minVerticalAngle = -30, maxVerticalAngle = 60;
|
|
public float minHorizontalAngle = -60, maxHorizontalAngle = 60;
|
|
[Header("初始化数据"), SerializeField] float orginDis = 0;
|
|
Vector3 currentPosition, movVelocity, targetPosition;
|
|
float targetDis, currentDis, disVelocity;
|
|
Vector3 currentRotation, targetRotation, rotVelocity;
|
|
[SerializeField] Transform target;
|
|
Vector3 pos;
|
|
bool isok = false;
|
|
public bool ZhoLI = false;//是否是重力模式
|
|
float speed = 20;
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
SetCameraTrans();
|
|
//Debug.Log("CameraControl");
|
|
}
|
|
[ContextMenu("设置相机位置")]
|
|
private void SetCameraTrans()
|
|
{
|
|
currentDis = targetDis = orginDis;
|
|
currentPosition = targetPosition = transform.position;
|
|
currentRotation = targetRotation = transform.localEulerAngles;
|
|
}
|
|
public void SetOrginDis(Vector3 orginPos, Vector3 orginRO)
|
|
{
|
|
target = null;
|
|
targetPosition = orginPos;
|
|
targetRotation = orginRO;
|
|
targetDis = 0;
|
|
}
|
|
public void SetPos(Vector3 pos,Vector3 ro)
|
|
{
|
|
targetPosition = pos;
|
|
targetRotation = ro;
|
|
}
|
|
private void Update()
|
|
{
|
|
if (Input.touchCount == 0 && Input.GetMouseButton(1))
|
|
{
|
|
targetRotation.y += Input.GetAxis("Mouse X") * mouseRotSpeed;
|
|
targetRotation.x -= Input.GetAxis("Mouse Y") * mouseRotSpeed;
|
|
}
|
|
targetDis = targetDis - Input.GetAxis("Mouse ScrollWheel") * mouseZoomSpeed;
|
|
targetRotation.x = Mathf.Clamp(targetRotation.x, minVerticalAngle, maxVerticalAngle);
|
|
//targetRotation.y = Mathf.Clamp(targetRotation.y, minHorizontalAngle, maxHorizontalAngle);
|
|
targetDis = Mathf.Clamp(targetDis, minDis, maxDis);
|
|
currentDis = Mathf.SmoothDamp(currentDis, targetDis, ref disVelocity, rSmoothTime);
|
|
currentRotation = Vector3.SmoothDamp(currentRotation, targetRotation, ref rotVelocity, rSmoothTime);
|
|
var tmpRotation = Quaternion.Euler(currentRotation);
|
|
var offset = tmpRotation * Vector3.forward * -currentDis;
|
|
if (target != null)
|
|
{
|
|
targetPosition = target.position;
|
|
}
|
|
currentPosition = Vector3.SmoothDamp(currentPosition, targetPosition, ref movVelocity, mSmoothTime);
|
|
var tmpPosition = currentPosition + offset;
|
|
if (transform.rotation != tmpRotation)
|
|
{
|
|
transform.rotation=tmpRotation;
|
|
}
|
|
if (ZhoLI)
|
|
{
|
|
if (transform.position.y > 3f)
|
|
{
|
|
transform.position = new Vector3(transform.position.x, 2.1f, transform.position.z);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (transform.position.y > 20f)
|
|
{
|
|
transform.position = new Vector3(transform.position.x, 20f, transform.position.z);
|
|
}
|
|
}
|
|
if (transform.position.y < 2f)
|
|
{
|
|
transform.position = new Vector3(transform.position.x, 2.1f, transform.position.z);
|
|
}
|
|
if (Input.GetKey(KeyCode.LeftShift))
|
|
{
|
|
speed = 25f;
|
|
}
|
|
else
|
|
{
|
|
speed = 12f;
|
|
}
|
|
|
|
|
|
float x= Mathf.Clamp(transform.position.x, -137f, 102f);
|
|
//float y = Mathf.Clamp(transform.position.y, 2.1f, 20f);
|
|
float z= Mathf.Clamp(transform.position.z, -125f, 119f);
|
|
transform.position = new Vector3(x,transform.position.y,z);
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
float x = Input.GetAxis("Horizontal");
|
|
float y = Input.GetAxis("Vertical");
|
|
float z = Input.GetAxis("Updown");
|
|
if (x != 0 || y != 0 || z != 0)
|
|
{
|
|
Debug.Log("Horizontal " + x + " Ver" + y);
|
|
if (!ZhoLI)
|
|
{
|
|
z = (z * Time.deltaTime * speed);
|
|
Debug.Log(z);
|
|
}
|
|
pos = new Vector3(x * Time.deltaTime * speed, z, y * Time.deltaTime * speed);
|
|
isok = true;
|
|
|
|
}
|
|
else
|
|
{
|
|
isok = false;
|
|
}
|
|
if (isok)
|
|
{
|
|
if (transform.position != pos || x != 0 || y != 0||z !=0)
|
|
{
|
|
transform.Translate(pos);
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
//public void Fly()
|
|
//{
|
|
// ZhoLI = false;
|
|
// transform.GetComponent<Rigidbody>().useGravity = false;
|
|
// transform.position = new Vector3(transform.position.x, 20f, transform.position.z);
|
|
//}
|
|
//public void NotFly()
|
|
//{
|
|
// ZhoLI = true;
|
|
// transform.GetComponent<Rigidbody>().useGravity = true;
|
|
// transform.position = new Vector3(transform.position.x,2.1f,transform.position.z);
|
|
//}
|
|
}
|
|
////声明部分public float horizontalinput;
|
|
/////水平参数public float Verticalinput;
|
|
/////垂直参数float speed=10.0f;
|
|
/////声明一个参数,没有规定
|
|
/////在update中书写
|
|
///void Update()
|
|
///{
|
|
///horizontalinput = Input.GetAxis("Horizontal");
|
|
/////AD方向控制Verticalinput = Input.GetAxis("Vertical");
|
|
///if (horizontalinput!=0&&Verticalinput!=0)
|
|
///{horizontalinput = horizontalinput * 0.6f;Verticalinput = Verticalinput * 0.6f;}
|
|
/////WS方向控制this.transform.Translate(Vector3.right * horizontalinput * Time.deltaTime * speed);
|
|
///控制该物体向侧方移动this.transform.Translate(Vector3.forward* Verticalinput * Time.deltaTime * speed);//控制该物体向前后移动} |