Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Drawing a box around a world object transform postion via canvas screen overlay

Discussion in 'Scripting' started by irjason, Mar 3, 2018.

  1. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Hi Guys,

    I am working on a targeting system for a little fun project i am making, I would like it to display a "image" cross hair on my overlay canvas around an object depended on there position in the world. Currently i have it half working, where i can track a enemy's ship but the issue is i only want it to be tracked if it is with in the Camera's current FOV. Right now it will show up if its directly behind me or in front of me, where i just want it to show when it's in front of me with in the FOV of the camera.

    This is what i have so far


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class CanvasBox : MonoBehaviour {
    7.  
    8.     public Transform FollowThis;
    9.     public PlayerInfo pi;
    10.     public Image image;
    11.  
    12.     private void Start()
    13.     {
    14.    
    15.         //Don't show on load.
    16.         image.enabled = false;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21. //If the player currently has a target
    22.         if (pi.GetHasTarget())
    23.         {
    24. //set the enemys target to the transform to track
    25.             FollowThis = pi.GetTarget().transform;
    26. //enable the image (might be bad practice to do in a update loop)  Should add to player controller bool check targeted later.
    27.             image.enabled = true;
    28. //if there is a target.
    29.             if (FollowThis == null)
    30.             {
    31.                 return;
    32.             }
    33. //Get the vector 2 of the position of the enemy via world space
    34.             Vector2 sp = Camera.main.WorldToScreenPoint(FollowThis.position);
    35. //edit the transform to match the world postion
    36.             this.transform.position = sp;
    37.         }
    38.     }
    39.  
    40. //setter of if it has a target
    41.     public void NoTarget()
    42.     {
    43.         image.enabled = false;
    44.     }
    45. //make sure there is a game object in our hierarchy selected
    46.     void OnEnable()
    47.     {
    48.         if (gameObject.activeInHierarchy)
    49.         {
    50.             Update();
    51.         }
    52.     }
    53. }
    54.  
    effectively it will show if either its directly in front of me or behind me and track it's movements. I presume this is simple but i am having a bit of a block at the moment. Presume it will be something like.

    //Sudo Code
    If Camera.fov has Target in its view.. then show image, else don't.

    Thanks in advance!
     
  2. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Ow my bad i got it, just realized what i was doing wrong, I should also have been checking the Z axis to see if it was >= 0, to see if it was in front or behind me.

    if (Camera.main.WorldToViewportPoint(FollowThis.position).z >= 0)
    {
    //In front of the camera!
    }

    Thanks anyway guys.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    If you want additional insight on this topics I suggest looking up quill18creates selecting units on YouTube.
     
  4. irjason

    irjason

    Joined:
    Sep 15, 2012
    Posts:
    42
    Thanks for the suggestion, I made a few improvements to my system if you would like to see it let me know, added the ability to track target's off screen etc.

    Here is a little clip of where i got to with it today, just messing around a bit for fun :)

     
    Last edited: Mar 4, 2018