DongYingLiangGuanYiGong/DongYing/Assets/Zion/Scripts/TerrainCam.cs

120 lines
3.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainCam : MonoBehaviour
{
public Camera cam;
private Vector3 offset;
public Vector3 target;
public float scaleMin = 40;
public float scaleMax = 60;
public float scaleValue = 60;
public float rotateValueX = 0;
public float rotateValueY = 0;
public float updownRot = 15;
public Quaternion rotationTarget = Quaternion.identity;
public static bool Active { get; set; } = true;
private void Update()
{
offset = new Vector3(0, Mathf.Pow(1 - updownRot / 90, 4) * 22f, 0);
Vector3 targetH = target + Vector3.up * 1000;
Ray ray = new Ray(targetH, -Vector3.up * 5000);
if (Physics.Raycast(ray, out var hit, 10000, 1 << 8))
{
target = hit.point;
}
else
{
target = new Vector3(0, 10, 0);
}
float mouseX = SInput.GetAxis("Mouse X");
float mouseY = SInput.GetAxis("Mouse Y");
if ((Input.GetMouseButton(0) && !SInput.IsTouch2AndControlRot) && Active)
{
float mv = (scaleValue - scaleMin) / scaleMax + 0.3f;
target += cam.transform.right * -mouseX * 1100 * Time.deltaTime * mv;
target += transform.forward * -mouseY * 1100 * Time.deltaTime * mv;
}
transform.position = Vector3.Lerp(transform.position, target + offset, Time.deltaTime * 3);
if (Active)
{
float msw = SInput.GetAxis("Mouse ScrollWheel");
scaleValue -= msw * 7500 * Time.deltaTime;
scaleValue = Mathf.Clamp(scaleValue, scaleMin, scaleMax);
}
float centerOffset = -16;
cam.transform.localPosition = Vector3.Lerp(cam.transform.localPosition, new Vector3(0, Mathf.Tan(updownRot * Mathf.PI / 180), -1).normalized * scaleValue * 2f, Time.deltaTime * 6);
if ((Input.GetMouseButton(1) || SInput.IsTouch2AndControlRot) && Active)
{
if (rotateValueY * mouseX < 0) rotateValueY = 0;
rotateValueY += Time.deltaTime * mouseX * 20;
rotateValueY = Mathf.Clamp(rotateValueY, -8, 8);
updownRot -= Time.deltaTime * mouseY * 200;
updownRot = Mathf.Clamp(updownRot, 15, 80);
if (rotateValueX * mouseY < 0) rotateValueX = 0;
rotateValueX += Time.deltaTime * -mouseY * 200;
rotateValueX = Mathf.Clamp(rotateValueX, -8, 8);
}
rotateValueY = Mathf.Lerp(rotateValueY, 0, Time.deltaTime * 3f);
rotateValueX = Mathf.Lerp(rotateValueX, 0, Time.deltaTime * 3f);
if (rotateValueY != 0)
{
Quaternion rot = Quaternion.Euler(0, rotateValueY * Time.deltaTime * 60, 0);
rotationTarget *= rot;
transform.localRotation *= rot;
}
//if (rotateValueX != 0)
//{
// updownRot += rotateValueX;
// updownRot = Mathf.Clamp(updownRot, 0.0f, 89.5f);
//}
transform.localRotation = Quaternion.Lerp(transform.localRotation, rotationTarget, Time.deltaTime * 6);
cam.transform.localRotation = Quaternion.Lerp(cam.transform.localRotation, Quaternion.Euler(new Vector3(updownRot, 0, 0)), Time.deltaTime * 6);
}
public void Set(Vector3 target, Vector3 rotationTarget, float scaleValue, float updownRot)
{
this.target = target;
this.updownRot = updownRot;
this.rotationTarget = Quaternion.Euler(rotationTarget);
this.scaleValue = scaleValue;
}
public void Set(Vector3 target, Quaternion rotationTarget, float scaleValue, float updownRot)
{
this.target = target;
this.updownRot = updownRot;
this.rotationTarget = rotationTarget;
this.scaleValue = scaleValue;
}
public void SetLook(Vector3 target, float scaleValue)
{
this.target = target;
this.scaleValue = scaleValue;
}
}