119 lines
3.3 KiB
C#
119 lines
3.3 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 = 75f;
|
||
private float moveSpeed = 6.0f; // 移动速度
|
||
private float rotationSpeed = 3.0f; // 旋转速度
|
||
|
||
private bool isMoving = false;
|
||
public static bool isStaticMove = false;
|
||
public static bool a=false;
|
||
void Start()
|
||
{
|
||
targetRotation = targetPosition;
|
||
}
|
||
|
||
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();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 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为
|
||
*
|
||
*/
|
||
|
||
// 启动移动过程
|
||
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;
|
||
}
|
||
}
|
||
|
||
IEnumerator MoveToTarget2()
|
||
{
|
||
if (isStaticMove)
|
||
{
|
||
isStaticMove = false;
|
||
}
|
||
yield return new WaitForSeconds(0.1f);
|
||
isMoving = true;
|
||
isStaticMove = true;
|
||
}
|
||
|
||
public void OnPointerClick(PointerEventData eventData)
|
||
{
|
||
|
||
print("点击了相机111");
|
||
if (!isStaticMove)
|
||
{
|
||
print("点击了相机");
|
||
MoveToTarget();
|
||
|
||
}
|
||
}
|
||
|
||
public void OnMouseDown()
|
||
{
|
||
if (!isStaticMove)
|
||
{
|
||
print("点击了相机");
|
||
MoveToTarget();
|
||
}
|
||
}
|
||
}
|