Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice

[SOLVED] Unity offscreen target indicator - little problem

Discussion in 'Scripting' started by polan31, Oct 11, 2018.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    UNITY 2D C#

    Hi everyone.
    I have a script that allows the arrow to show where the point is .

    I have two problems:

    1) I would like to get arrow much closer to the edge of the camera, because now it is too far (how you can see on picture);

    2) I would like the arrow to show the way to a point with a specific tag or name.

    How to do it?

    Could anyone help me transform the script?

    PS. Project for android, screen 854x480.

    My script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CodeMonkey.Utils;
    5.  
    6.  
    7. public class Window_QuestPointer : MonoBehaviour {
    8.  
    9.     [SerializeField] private Camera uiCamera;
    10.  
    11.  
    12.  
    13.     private Vector3 targetPosition;
    14.     private RectTransform pointerRectTransform;
    15.  
    16.     private void Awake ()
    17.     {
    18.         targetPosition = new Vector3 (50, 0);
    19.         pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
    20.     }
    21.     private void Update (){
    22.         Vector3 toPosition = targetPosition;
    23.         Vector3 fromPosition = Camera.main.transform.position;
    24.         fromPosition.z = 0f;
    25.         Vector3 dir = (toPosition - fromPosition).normalized;
    26.         float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    27.         pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);
    28.  
    29.         float borderSize = 100f;
    30.    
    31.         Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    32.         bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    33.         Debug.Log (isOffscreen + " " + targetPositionScreenPoint);
    34.  
    35.         if(isOffscreen){
    36.             Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
    37.             cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
    38.             cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);
    39.  
    40.             Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
    41.             pointerRectTransform.position = pointerWorldPosition;
    42.             pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);
    43.  
    44.         }
    45.         else{
    46.             Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
    47.             pointerRectTransform.position = pointerWorldPosition;
    48.             pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);
    49.  
    50.     }
    51. }
    52. }
    53.            
    Arrow.png
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,646
    I think you need to lower this 100f to achieve your goal. (I may be wrong, I haven't run this script in Unity)
     
    polan31 likes this.
  3. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    OK you're right.

    So far, I have been increasing the number, so the arrow has disappeared from the screen.

    Now it work well :)

    But the second problem remains:

    How to make an arrow point to an object with a specific tag?
     
  4. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Solved:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CodeMonkey.Utils;
    5.  
    6.  
    7. public class Window_QuestPointer : MonoBehaviour {
    8.  
    9.     [SerializeField] private Camera uiCamera;
    10.  
    11.  
    12.  
    13.     private Vector3 targetPosition;
    14.     private RectTransform pointerRectTransform;
    15.  
    16.     private void Awake ()
    17.     {
    18.         targetPosition = GameObject.FindWithTag("Star").transform.position;
    19.         pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
    20.     }
    21.     private void Update (){
    22.         Vector3 toPosition = targetPosition;
    23.         Vector3 fromPosition = Camera.main.transform.position;
    24.         fromPosition.z = 0f;
    25.         Vector3 dir = (toPosition - fromPosition).normalized;
    26.         float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    27.         pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);
    28.  
    29.         float borderSize = 40f;
    30.    
    31.         Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    32.         bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    33.         Debug.Log (isOffscreen + " " + targetPositionScreenPoint);
    34.  
    35.         if(isOffscreen){
    36.             Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
    37.             cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
    38.             cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);
    39.  
    40.             Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
    41.             pointerRectTransform.position = pointerWorldPosition;
    42.             pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);
    43.  
    44.         }
    45.         else{
    46.             Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
    47.             pointerRectTransform.position = pointerWorldPosition;
    48.             pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);
    49.  
    50.     }
    51. }
    52. }
    53.            
     
unityunity