96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.EventSystems;
|
|||
|
|
|
|||
|
|
public class CameraMove : MonoBehaviour, IPointerClickHandler
|
|||
|
|
{
|
|||
|
|
public Transform targetPosition; // 目标位置
|
|||
|
|
public Transform targetRotation; // 目标旋转(或直接指定一个 Quaternion)
|
|||
|
|
public GameObject targetObject; // 目标对象(如果有)
|
|||
|
|
public Camera camera; // 相机
|
|||
|
|
public int cameraView = 0; // 相机视角
|
|||
|
|
public float cameraSize = 60f;
|
|||
|
|
private float moveSpeed = 6.0f; // 移动速度
|
|||
|
|
private float rotationSpeed = 3.0f; // 旋转速度
|
|||
|
|
|
|||
|
|
private bool isMoving = false;
|
|||
|
|
public static bool isStaticMove = false;
|
|||
|
|
void Start()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
if (isMoving)
|
|||
|
|
{
|
|||
|
|
// 平滑移动
|
|||
|
|
camera.transform.position = Vector3.Lerp(camera.transform.position, targetPosition.position, moveSpeed * Time.deltaTime);
|
|||
|
|
|
|||
|
|
// 平滑旋转
|
|||
|
|
camera.transform.rotation = Quaternion.Slerp(camera.transform.rotation, targetRotation.rotation, rotationSpeed * Time.deltaTime);
|
|||
|
|
|
|||
|
|
// 检查是否接近目标位置和旋转
|
|||
|
|
if (Vector3.Distance(camera.transform.position, targetPosition.position) < 0.1f &&
|
|||
|
|
Quaternion.Angle(camera.transform.rotation, targetRotation.rotation) < 0.1f)
|
|||
|
|
{
|
|||
|
|
camera.transform.position = targetPosition.position;
|
|||
|
|
camera.transform.rotation = targetRotation.rotation;
|
|||
|
|
isMoving = false;
|
|||
|
|
isStaticMove = false;
|
|||
|
|
|
|||
|
|
if (targetObject!= null)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
if (gameObject.GetComponent<ShowHidden>()!=null)
|
|||
|
|
{
|
|||
|
|
gameObject.GetComponent<ShowHidden>().OnMouseDown();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 启动移动过程
|
|||
|
|
public void MoveToTarget()
|
|||
|
|
{
|
|||
|
|
isMoving = true;
|
|||
|
|
isStaticMove = true;
|
|||
|
|
if (cameraView == 0)
|
|||
|
|
{
|
|||
|
|
camera.orthographic = false;
|
|||
|
|
camera.fieldOfView = cameraSize;
|
|||
|
|
}
|
|||
|
|
if (cameraView == 1)
|
|||
|
|
{
|
|||
|
|
camera.orthographic=true;
|
|||
|
|
camera.orthographicSize=10;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void OnPointerClick(PointerEventData eventData)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
print("点击了相机111");
|
|||
|
|
if (!isStaticMove)
|
|||
|
|
{
|
|||
|
|
print("点击了相机");
|
|||
|
|
MoveToTarget();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void OnMouseDown()
|
|||
|
|
{
|
|||
|
|
if (!isStaticMove)
|
|||
|
|
{
|
|||
|
|
print("点击了相机");
|
|||
|
|
MoveToTarget();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|