NG_/Assets/Scripts/3D/摄像机/CameraMove.cs

119 lines
3.3 KiB
C#
Raw Permalink Normal View History

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开始移动1true进不来
*
* A移动时2为true
* B开始移动1trueA不移动B开始移动
* static1设为false并且2为true,12true时就可以移动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();
}
}
}