Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Draw texture on object, with a rotating camera

Discussion in 'Scripting' started by Threstle, Oct 19, 2014.

  1. Threstle

    Threstle

    Joined:
    Mar 7, 2013
    Posts:
    1
    Hi everyone,

    I'm making a first person spatial shooter in cockpit view. I need a lock on system to track ennemies on screen. My first idea was to get the position of the object I need to lock on, convert it to screen position, and draw on that, like that :

    Code (CSharp):
    1.  
    2. void OnGUI() {
    3.             Vector3 position = Camera.main.WorldToScreenPoint (target.transform.position);
    4.             GUI.DrawTexture (new Rect (position.x,position.y, 0, 0), lockImage);
    5. }

    Unfortunatly, as the camera is always rotating (chasing ennemies in space, and stuff), it's not enough. I tried to correct the position with rotation matrix, but I don't seem to get it right. Is it the way to go ? Or is there another, easier, solution ?

    Thanks,

    Etienne