Search Unity

UI elements face to camera

Discussion in 'Scripting' started by JamesButlerI, Oct 6, 2015.

  1. JamesButlerI

    JamesButlerI

    Joined:
    Oct 4, 2015
    Posts:
    8
    Hi all together!

    I want to create healthbars as a child of enemies, that fly above them and always face the main camera.
    the camera is orthographic and will never change its direction.
    i tried this script which is a component of the canvas on which the slider etc. will be drawn.
    Code (CSharp):
    1. public class CanvasController : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.        transform.rotation=Camera.main.transform.rotation;
    6.     }
    7. }
    this code technically works but since the canvas is a child of the enemy gameobject it will automaticly take its rotation and the canvas will wriggle when the enemy rotation changes.

    the movement of the enemy will also be done in an update() method (not in fixedupdate()) in case that matters.

    thanks for helping out!

    James Butler
     
    Plaximo and hballamco like this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Put it in LateUpdate instead. That will happen after all the other movement has finished.
     
    Plaximo likes this.
  3. JamesButlerI

    JamesButlerI

    Joined:
    Oct 4, 2015
    Posts:
    8
    thank you it works now.