144 lines
5.9 KiB
C#
144 lines
5.9 KiB
C#
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using UnityEngine;
|
||
|
||
public class ClickToFocus : MonoBehaviour
|
||
{
|
||
//[Tooltip("相机在目标物体位置的偏移量")] public Vector3 cameraOffset = new Vector3(5f, 10f, 0f);
|
||
private Dictionary<string, Vector3> targetObjectAndPosition = new Dictionary<string, Vector3>();//尝试使用字典数据类型来定义每一个物体自己对应的观察点位置
|
||
|
||
[Tooltip("平滑过渡所用时间(秒)")] public float transitionDuration = 1.0f;
|
||
[Header("双击设置")]
|
||
[Tooltip("双击间隔时间阈值(秒)")] public float doubleClickInterval = 0.3f;
|
||
private float lastClickTime = 0f;
|
||
private bool waitingSecondClick = false;
|
||
|
||
private Camera mainCamera;
|
||
|
||
public Vector3 core = new Vector3(-90f, 7.2f, -19.9f);
|
||
|
||
//public GameObject lastObj;
|
||
//public GameObject currentObj;
|
||
|
||
private void Awake()
|
||
{
|
||
core = new Vector3(-90f, 7.2f, -19.9f);
|
||
}
|
||
void Start()
|
||
{
|
||
mainCamera = Camera.main;//主相机的那个Camera组件跳转到的观测点
|
||
targetObjectAndPosition.Add("sb_cexieyi001", new Vector3(-72.4f, 7.2f, -19.9f));
|
||
targetObjectAndPosition.Add("sb_cexieyi003", new Vector3(-90f, 7.2f, -19.9f));
|
||
targetObjectAndPosition.Add("sb_cexieyi005", new Vector3(-145.5f, 7.2f, -19.9f));
|
||
targetObjectAndPosition.Add("sb_cexieyi007", new Vector3(-167.3f, 7.2f, -19.9f));
|
||
targetObjectAndPosition.Add("sb_cexieyi002", new Vector3(-72.4f, 13.1f, -28f));
|
||
targetObjectAndPosition.Add("sb_cexieyi004", new Vector3(-90f, 13.1f, -28f));
|
||
targetObjectAndPosition.Add("sb_cexieyi006", new Vector3(-146.4f, 13.1f, -28f));
|
||
targetObjectAndPosition.Add("sb_cexieyi008", new Vector3(-167f, 13.1f, -28f));
|
||
|
||
targetObjectAndPosition.Add("sb_gnss001", new Vector3(-68.2f, 8.3f, -20f));
|
||
targetObjectAndPosition.Add("sb_gnss003", new Vector3(-88.6f, 8.3f, -20f));
|
||
targetObjectAndPosition.Add("sb_gnss005", new Vector3(-141.8f, 8.3f, -20f));
|
||
targetObjectAndPosition.Add("sb_gnss007", new Vector3(-165.8f, 8.3f, -20f));
|
||
targetObjectAndPosition.Add("sb_gnss002", new Vector3(-68f, 14.3f, -28f));
|
||
targetObjectAndPosition.Add("sb_gnss004", new Vector3(-88.9f, 14.3f, -28f));
|
||
targetObjectAndPosition.Add("sb_gnss006", new Vector3(-141.7f, 14.3f, -28f));
|
||
targetObjectAndPosition.Add("sb_gnss008", new Vector3(-165.8f, 14.3f, -28f));
|
||
|
||
targetObjectAndPosition.Add("sb_lashengweiyi001", new Vector3(-68.7f, 7.2f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_lashengweiyi002", new Vector3(-92.6f, 7.2f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_lashengweiyi003", new Vector3(-143.4f, 7.2f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_lashengweiyi004", new Vector3(-170.5f, 7.2f, -20.7f));
|
||
|
||
targetObjectAndPosition.Add("sb_maosuoji001", new Vector3(-67.7f, 8.1f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_maosuoji002", new Vector3(-94.7f, 8.1f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_maosuoji003", new Vector3(-142f, 8.1f, -20.7f));
|
||
targetObjectAndPosition.Add("sb_maosuoji004", new Vector3(-172f, 8.1f, -20.7f));
|
||
|
||
targetObjectAndPosition.Add("sb_qixiangjiankong001", new Vector3(-84.7f, 15f, -28f));
|
||
targetObjectAndPosition.Add("sb_qixiangjiankong002", new Vector3(-161f, 15f, -28f));
|
||
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
if (Input.GetMouseButtonDown(0))
|
||
{
|
||
if (waitingSecondClick && Time.time - lastClickTime <= doubleClickInterval)
|
||
{
|
||
HandleClick();
|
||
//core = transform.position + new Vector3(-5f, 0f, 0f);
|
||
waitingSecondClick = false;
|
||
}
|
||
else
|
||
{
|
||
waitingSecondClick = true;
|
||
lastClickTime = Time.time;
|
||
StartCoroutine(ResetClickState());
|
||
}
|
||
}
|
||
}
|
||
|
||
private IEnumerator ResetClickState()
|
||
{
|
||
yield return new WaitForSeconds(doubleClickInterval);
|
||
waitingSecondClick = false;
|
||
}
|
||
|
||
private void HandleClick()
|
||
{
|
||
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity) && hit.collider.CompareTag("Target"))//这里有点小问题,应该给被打中的物体加一个标签,不然正常物体也会影响core
|
||
{
|
||
//出一个hit
|
||
foreach (KeyValuePair<string, Vector3> pair in targetObjectAndPosition)
|
||
{
|
||
if (hit.transform.gameObject.name == pair.Key)
|
||
{
|
||
Vector3 targetPos = pair.Value;
|
||
StartCoroutine(MoveCamera(mainCamera, targetPos, transitionDuration, hit.transform));
|
||
}
|
||
}
|
||
core = hit.transform.position;
|
||
|
||
//中间夹着的代码为提供红色高亮显示的功能,和双击跳转是分开的
|
||
//if (lastObj == null)
|
||
//{
|
||
// currentObj = hit.collider.GetComponent<Transform>().gameObject;
|
||
// currentObj.layer = 6;
|
||
//}
|
||
|
||
//if (lastObj != null)
|
||
//{
|
||
// currentObj = hit.collider.GetComponent<Transform>().gameObject;
|
||
// currentObj.layer = 6;
|
||
// lastObj.layer = 0;
|
||
|
||
//}
|
||
|
||
// lastObj = currentObj;
|
||
//中间夹着的代码为提供红色高亮显示的功能,和双击跳转是分开的
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
private IEnumerator MoveCamera(Camera cam, Vector3 targetPos, float duration, Transform focusTarget)
|
||
{
|
||
Vector3 startPos = cam.transform.position;
|
||
float elapsed = 0f;
|
||
while (elapsed < duration)
|
||
{
|
||
float t = Mathf.Lerp(0f, 1f, elapsed / duration);
|
||
cam.transform.position = Vector3.Lerp(startPos, targetPos, t);
|
||
if (focusTarget != null)
|
||
cam.transform.LookAt(focusTarget);
|
||
elapsed += Time.deltaTime;
|
||
yield return null;
|
||
}
|
||
cam.transform.position = targetPos;
|
||
if (focusTarget != null)
|
||
cam.transform.LookAt(focusTarget);
|
||
}
|
||
} |