Unity offscreen target indicator-如何显示带有特定标签的对象的路径?

Unity offscreen target indicator-how to show the way to an object with a specific tag?

UNITY 2D C#

大家好。 我有一个脚本可以让箭头显示点在哪里。

我的问题:

1) 我希望箭头显示通往具有特定标签或名称的点的路径。

怎么做?

因为现在,显示了通往点 (-50,0) 的路。

谁能帮我改造脚本?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CodeMonkey.Utils;


public class Window_QuestPointer : MonoBehaviour {

[SerializeField] private Camera uiCamera;



private Vector3 targetPosition;
private RectTransform pointerRectTransform;

private void Awake ()
{
    targetPosition = new Vector3 (-50, 0);
    pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
}
private void Update (){
    Vector3 toPosition = targetPosition;
    Vector3 fromPosition = Camera.main.transform.position;
    fromPosition.z = 0f;
    Vector3 dir = (toPosition - fromPosition).normalized;
    float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);

    float borderSize = 40f;

    Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    Debug.Log (isOffscreen + " " + targetPositionScreenPoint);

    if(isOffscreen){
        Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
        cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
        cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);

        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

    }
    else{
        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

}

} }

  1. 标记项目 "target"

  2. 将此添加到 Awake 方法的末尾以获取目标

    虚空觉醒(){ Transform offscreen = GameObject.FindGameObjectWithTag("target").GetComponent(); 目标位置=offscreen.position; }

应该可以解决问题。我在 phone 上输入了这个,所以它应该是伪造的。让我知道!

解决问题。下面的好脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CodeMonkey.Utils;


public class Window_QuestPointer : MonoBehaviour {

[SerializeField] private Camera uiCamera;



private Vector3 targetPosition;
private RectTransform pointerRectTransform;

private void Awake ()
{
    targetPosition = GameObject.FindWithTag("Target").transform.position;
    pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
}
private void Update (){
    Vector3 toPosition = targetPosition;
    Vector3 fromPosition = Camera.main.transform.position;
    fromPosition.z = 0f;
    Vector3 dir = (toPosition - fromPosition).normalized;
    float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);

    float borderSize = 40f;

    Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    Debug.Log (isOffscreen + " " + targetPositionScreenPoint);

    if(isOffscreen){
        Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
        cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
        cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);

        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

    }
    else{
        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

}

} }