fix:代码提交
This commit is contained in:
@@ -4,17 +4,20 @@ 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 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;
|
||||
[Tooltip("平滑过渡所用时间(秒)")] public float transitionDuration = 1.0f;
|
||||
[Header("双击设置")]
|
||||
[Tooltip("双击间隔时间阈值(秒)")] public float doubleClickInterval = 0.3f;
|
||||
private float lastClickTime = 0f;
|
||||
private bool waitingSecondClick = false;
|
||||
|
||||
|
||||
private Camera mainCamera;
|
||||
|
||||
private ControlMove cameraCtrl;
|
||||
|
||||
public Vector3 core = new Vector3(-90f, 7.2f, -19.9f);
|
||||
|
||||
//public GameObject lastObj;
|
||||
@@ -23,10 +26,15 @@ public class ClickToFocus : MonoBehaviour
|
||||
private void Awake()
|
||||
{
|
||||
core = new Vector3(-90f, 7.2f, -19.9f);
|
||||
// 确保初始化方法被调用,避免cameraCtrl为空
|
||||
if (cameraCtrl == null)
|
||||
{
|
||||
cameraCtrl = GetComponent<ControlMove>();
|
||||
}
|
||||
}
|
||||
void Start()
|
||||
{
|
||||
mainCamera = Camera.main;//主相机的那个Camera组件跳转到的观测点
|
||||
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));
|
||||
@@ -88,20 +96,21 @@ public class ClickToFocus : MonoBehaviour
|
||||
private void HandleClick()
|
||||
{
|
||||
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity) && hit.collider.CompareTag("Target"))//这里有点小问题,应该给被打中的物体加一个标签,不然正常物体也会影响core
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity) && hit.collider.CompareTag("Target"))//这里有点小问题,应该给被打中的物体加一个标签,不然正常物体也会影响core
|
||||
{
|
||||
//出一个hit
|
||||
//出一个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;
|
||||
@@ -116,8 +125,8 @@ public class ClickToFocus : MonoBehaviour
|
||||
|
||||
//}
|
||||
|
||||
// lastObj = currentObj;
|
||||
//中间夹着的代码为提供红色高亮显示的功能,和双击跳转是分开的
|
||||
// lastObj = currentObj;
|
||||
//中间夹着的代码为提供红色高亮显示的功能,和双击跳转是分开的
|
||||
}
|
||||
|
||||
|
||||
@@ -126,19 +135,20 @@ public class ClickToFocus : MonoBehaviour
|
||||
|
||||
private IEnumerator MoveCamera(Camera cam, Vector3 targetPos, float duration, Transform focusTarget)
|
||||
{
|
||||
Vector3 startPos = cam.transform.position;
|
||||
Vector3 startPos = transform.position;
|
||||
cameraCtrl.GetInScriptCore(targetPos);
|
||||
float elapsed = 0f;
|
||||
while (elapsed < duration)
|
||||
{
|
||||
float t = Mathf.Lerp(0f, 1f, elapsed / duration);
|
||||
cam.transform.position = Vector3.Lerp(startPos, targetPos, t);
|
||||
transform.position = Vector3.Lerp(startPos, targetPos, t);
|
||||
if (focusTarget != null)
|
||||
cam.transform.LookAt(focusTarget);
|
||||
transform.LookAt(focusTarget);
|
||||
elapsed += Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
cam.transform.position = targetPos;
|
||||
transform.position = targetPos;
|
||||
if (focusTarget != null)
|
||||
cam.transform.LookAt(focusTarget);
|
||||
transform.LookAt(focusTarget);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user