适用于一体机待机界面的交互形式
编写了鼠标和触摸屏两种方式,脚本挂在相机上,相机要有一个父物体
using UnityEngine;
using DG.Tweening;
public class MouseLookCamera : MonoBehaviour
{
public bool isMouse = false;
public float sensitivityMouseX = 100F;
public float sensitivityMouseY = 100F;
public float sensitivityTouchX = 5F;
public float sensitivityTouchY = 5F;
private float minimumX = -5F;
private float maximumX = 5F;
private float minimumY = -5F;
private float maximumY = 5F;
public float smoothSpeedMouse = 3f;
public float smoothSpeedTouch = 30f;
private Transform playerBody;
[SerializeField]
private float rotationX = 0F;
[SerializeField]
private float rotationY = 0F;
private Tweener tweener1;
private Tweener tweener2;
void Start()
{
playerBody = transform.parent;
}
void Update()
{
if (isMouse)
{
if (Input.GetMouseButtonDown(0))
{
tweener1.Kill();
tweener2.Kill();
rotationX = transform.localEulerAngles.x;
if (rotationX > 180) rotationX -= 360;
rotationY = playerBody.transform.localEulerAngles.y;
if (rotationY > 180) rotationY -= 360;
}
if (Input.GetMouseButton(0))
{
float mouseX = Input.GetAxis("Mouse X") * sensitivityMouseX * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivityMouseY * Time.deltaTime;
rotationX += mouseY;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
rotationY -= mouseX;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Quaternion targetRotationA = Quaternion.Euler(rotationX, 0, 0);
Quaternion targetRotationB = Quaternion.Euler(0, rotationY, 0);
transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRotationA, smoothSpeedMouse * Time.deltaTime);
playerBody.localRotation = Quaternion.Lerp(playerBody.transform.localRotation, targetRotationB, smoothSpeedMouse * Time.deltaTime);
}
if (Input.GetMouseButtonUp(0))
{
ResetCamera();
}
}
else
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
tweener1.Kill();
tweener2.Kill();
rotationX = transform.localEulerAngles.x;
if (rotationX > 180) rotationX -= 360;
rotationY = playerBody.transform.localEulerAngles.y;
if (rotationY > 180) rotationY -= 360;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Touch touch = Input.GetTouch(0);
float touchSwipeX = touch.deltaPosition.x * sensitivityTouchX * Time.deltaTime;
float touchSwipeY = touch.deltaPosition.y * sensitivityTouchY * Time.deltaTime;
rotationX += touchSwipeY;
rotationX = Mathf.Clamp(rotationX, minimumX, maximumX);
rotationY -= touchSwipeX;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
Quaternion targetRotationA = Quaternion.Euler(rotationX, 0, 0);
Quaternion targetRotationB = Quaternion.Euler(0, rotationY, 0);
transform.localRotation = Quaternion.Lerp(transform.localRotation, targetRotationA, smoothSpeedTouch * Time.deltaTime);
playerBody.localRotation = Quaternion.Lerp(playerBody.transform.localRotation, targetRotationB, smoothSpeedTouch * Time.deltaTime);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
ResetCamera();
}
}
}
private void ResetCamera()
{
tweener1.Kill();
tweener2.Kill();
tweener1 = transform.DOLocalRotateQuaternion(Quaternion.Euler(Vector3.zero), 0.5f);
tweener2 = playerBody.transform.DOLocalRotateQuaternion(Quaternion.Euler(Vector3.zero), 0.5f);
}
}