126 lines
4.4 KiB
C#
126 lines
4.4 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(-219f, 311f, 268f);
|
||
|
||
|
||
|
||
|
||
public GameObject lastObj;
|
||
public GameObject currentObj;
|
||
|
||
|
||
void Start()
|
||
{
|
||
mainCamera = Camera.main;//主相机的那个Camera组件跳转到的观测点
|
||
targetObjectAndPosition.Add("bx_talou", new Vector3(-82f, 37f, -100f));
|
||
targetObjectAndPosition.Add("bx_langqiao", new Vector3(-35f, 30f, -59f));
|
||
targetObjectAndPosition.Add("bx_qiaozha", new Vector3(-26f, 35f, -74f));
|
||
targetObjectAndPosition.Add("bx_zhafang1", new Vector3(-64f, 21f, -102f));
|
||
targetObjectAndPosition.Add("bx_zhafang2", new Vector3(-8f, 26f, -173f));
|
||
targetObjectAndPosition.Add("bx_zhafang3", new Vector3(12f,23f,-37f));
|
||
targetObjectAndPosition.Add("bx_diaosu", new Vector3(-18f,18f,13f));
|
||
targetObjectAndPosition.Add("sb_shujucaijiyi", new Vector3(-26f,19f,11f));
|
||
targetObjectAndPosition.Add("sb_shujucaijiyi (1)", new Vector3(31f,17f,-60f));
|
||
targetObjectAndPosition.Add("bx_zhafang5", new Vector3(53f,26f,-23f));
|
||
targetObjectAndPosition.Add("pj_tingzi", new Vector3(67f,20f,-97f));
|
||
}
|
||
|
||
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);
|
||
}
|
||
} |