110 lines
4.9 KiB
C#
110 lines
4.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class ControllerOrbit : MonoBehaviour {
|
|
public bool isActive = false;
|
|
public bool isControllable = true;
|
|
public bool reverseXAxis = false;
|
|
public bool reverseYAxis = false;
|
|
public float minZoomAmount=1;
|
|
public float maxZoomAmount = 8;
|
|
public Transform cameraTarget;
|
|
public Vector2 mouseSensitivity = new Vector2(4.0f, 4.0f);
|
|
public Vector3 cameraOffset = new Vector3(0.0f, 0.0f, 0.0f);
|
|
public float cameraFOV = 60;
|
|
|
|
private ControllerMaster CM;
|
|
private InputController IC;
|
|
private Transform cameraObject;
|
|
private Vector3 targetPosition;
|
|
private Quaternion targetRotation;
|
|
private Vector2 axisSensitivity = new Vector2(4,4);
|
|
private float setFOV = 1;
|
|
private float camFOV = 60f;
|
|
private float oldMouseRotation;
|
|
private float MouseRotationDistance = 0;
|
|
private float MouseVerticalDistance = 0;
|
|
private float MouseScrollDistance = 0;
|
|
[HideInInspector]public static float followDistance=20;//摄像机初始距离与焦点,值越大摄像机越远
|
|
private float followTgtDistance = 0;
|
|
private float camRotation = 0;
|
|
private float camHeight = 4;
|
|
|
|
private bool orbitView = false;
|
|
private bool isExtraZoom = false;
|
|
|
|
public static bool isFoucs = false;
|
|
private void Awake()
|
|
{
|
|
targetPosition = transform.position;
|
|
targetRotation = Quaternion.Euler(new Vector3(-90,0,0));
|
|
|
|
CM = GetComponent<ControllerMaster>();
|
|
IC = GetComponent<InputController>();
|
|
}
|
|
private void FixedUpdate()
|
|
{
|
|
if (isActive)
|
|
{
|
|
cameraObject = CM.cameraObject;
|
|
if (isControllable)
|
|
{
|
|
//isExtraZoom = IC.inputMouseKey1;
|
|
setFOV = isExtraZoom ? 0.5f : 1;
|
|
orbitView = IC.inputMouseKey0 || IC.inputMouseKey1;
|
|
}
|
|
targetPosition = cameraTarget.position;
|
|
oldMouseRotation = MouseRotationDistance;
|
|
|
|
MouseRotationDistance = IC.udpRotX ;
|
|
MouseVerticalDistance = IC.udpRotY;
|
|
MouseScrollDistance = IC.udpMoveY / 5;
|
|
if (reverseXAxis) MouseRotationDistance = -IC.udpRotX ;
|
|
if (reverseYAxis) MouseVerticalDistance = -IC.udpRotY ;
|
|
camFOV = Mathf.Lerp(camFOV, cameraFOV * setFOV, Time.deltaTime * 4.0f);
|
|
|
|
axisSensitivity = new Vector2(
|
|
Mathf.Lerp(axisSensitivity.x, mouseSensitivity.x, Time.deltaTime * 4.0f),
|
|
Mathf.Lerp(axisSensitivity.y, mouseSensitivity.y, Time.deltaTime * 4.0f)
|
|
);
|
|
if (isControllable)
|
|
{
|
|
float followLerpSpeed = 7.0f;
|
|
followDistance -= MouseScrollDistance * 2.0f;
|
|
followDistance = Mathf.Clamp(followDistance, minZoomAmount, maxZoomAmount);
|
|
followTgtDistance = Mathf.Lerp(followTgtDistance, followDistance, Time.deltaTime * followLerpSpeed);
|
|
|
|
//if (orbitView) camRotation = Mathf.Lerp(oldMouseRotation, MouseRotationDistance * axisSensitivity.x, Time.deltaTime);
|
|
camRotation = Mathf.Lerp(oldMouseRotation, MouseRotationDistance * axisSensitivity.x, Time.deltaTime);
|
|
targetRotation.eulerAngles = new Vector3(
|
|
targetRotation.eulerAngles.x,
|
|
targetRotation.eulerAngles.y + camRotation,
|
|
targetRotation.eulerAngles.z
|
|
);
|
|
cameraObject.transform.eulerAngles = new Vector3(
|
|
targetRotation.eulerAngles.x,
|
|
targetRotation.eulerAngles.y,
|
|
cameraObject.transform.eulerAngles.z
|
|
);
|
|
//if (orbitView) camHeight = Mathf.Lerp(camHeight, camHeight + MouseVerticalDistance * axisSensitivity.y, Time.deltaTime);
|
|
camHeight = Mathf.Lerp(camHeight, camHeight + MouseVerticalDistance * axisSensitivity.y, Time.deltaTime);
|
|
camHeight = Mathf.Clamp(camHeight, -20, 20);
|
|
if (isFoucs)
|
|
{
|
|
followDistance = 10;
|
|
camHeight = cameraTarget.transform.position.y;
|
|
isFoucs = false;
|
|
}
|
|
cameraObject.transform.position = new Vector3(
|
|
cameraTarget.transform.position.x + cameraOffset.x + (-cameraObject.transform.up.x * followTgtDistance),
|
|
Mathf.Lerp(camHeight, cameraTarget.transform.position.y + cameraOffset.y + (-cameraObject.transform.up.y * followTgtDistance), Time.deltaTime * 0.5f),
|
|
cameraTarget.transform.position.z + cameraOffset.z + (-cameraObject.transform.up.z * followTgtDistance)
|
|
);
|
|
cameraObject.transform.LookAt(new Vector3(targetPosition.x, targetPosition.y , targetPosition.z));
|
|
cameraObject.GetComponent<Camera>().fieldOfView = camFOV;
|
|
}
|
|
}
|
|
}
|
|
}
|