2024-12-13 19:40:05 +08:00
|
|
|
|
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; // 相机视角
|
2025-01-10 16:52:52 +08:00
|
|
|
|
public float cameraSize = 75f;
|
2024-12-13 19:40:05 +08:00
|
|
|
|
private float moveSpeed = 6.0f; // 移动速度
|
|
|
|
|
|
private float rotationSpeed = 3.0f; // 旋转速度
|
|
|
|
|
|
|
|
|
|
|
|
private bool isMoving = false;
|
|
|
|
|
|
public static bool isStaticMove = false;
|
2025-01-10 16:52:52 +08:00
|
|
|
|
public static bool a=false;
|
2024-12-13 19:40:05 +08:00
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
2025-01-10 16:52:52 +08:00
|
|
|
|
targetRotation = targetPosition;
|
2024-12-13 19:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-10 16:52:52 +08:00
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
* 1 static 、 2
|
|
|
|
|
|
*
|
|
|
|
|
|
* 当A移动时2为true
|
|
|
|
|
|
* B开始移动,发现1为true进不来
|
|
|
|
|
|
*
|
|
|
|
|
|
* 当A移动时2为true
|
|
|
|
|
|
* B开始移动,发现1为true,实现A不移动B开始移动
|
|
|
|
|
|
* 将static1设为false并且2为true,1、2任意为true时就可以移动,当2为
|
|
|
|
|
|
*
|
|
|
|
|
|
*/
|
2024-12-13 19:40:05 +08:00
|
|
|
|
|
|
|
|
|
|
// 启动移动过程
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-01-10 16:52:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IEnumerator MoveToTarget2()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isStaticMove)
|
|
|
|
|
|
{
|
|
|
|
|
|
isStaticMove = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
|
|
isMoving = true;
|
|
|
|
|
|
isStaticMove = true;
|
2024-12-13 19:40:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
print("点击了相机111");
|
|
|
|
|
|
if (!isStaticMove)
|
|
|
|
|
|
{
|
|
|
|
|
|
print("点击了相机");
|
|
|
|
|
|
MoveToTarget();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void OnMouseDown()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!isStaticMove)
|
|
|
|
|
|
{
|
|
|
|
|
|
print("点击了相机");
|
|
|
|
|
|
MoveToTarget();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|