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

Rotating billboard script...

Discussion in 'Scripting' started by guategeek_legacy, Nov 5, 2005.

  1. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Would anyone be able to give me a script that would rotate a billboard so that it is always facing the camera? Jeff
     
  2. hangt5

    hangt5

    Joined:
    Oct 26, 2005
    Posts:
    26
    I imagine the most obvious would be:

    void Update()
    {
    transform.LookAt(Camera.current.transform);
    }
     
  3. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Ok so I copy and pasted that into a .js file and I get this error:

    Assets/Camera rotate script.js(4) error BCE0044 at column 1: 'expecting EOF, found '}".

    Sorry I'm such a ditz, but I'm 100% artist and 0% programer, so realy easy stuff sends me through a loop. Jeff
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Code (csharp):
    1. function Update()
    2. {
    3.     transform.LookAt(Camera.current.transform);
    4. }
    C# / JavaScript difference is all.

    -Jon
     
  5. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    OK I wondered if that was the problem, is there any way to know what language the script is written in (that amateurs can figure out :p ) so that when we copy a script off this forum we know what script language to use. Jeff
     
  6. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Don't know anything about Boo, but here's a way of telling JavaScript and C# apart, assuming you have a big enough snippet:

    JavaScript precedes function definitions with the word 'function'. You'll see the word 'var' in a lot of places.

    C# scripts precede function definitions with various things, but 'void' is something you'll commonly see. Also, if you see 'public class Name : MonoBehaviour' near the top of the source, it's C#.
     
  7. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Thanx Neil.

    Ok so I got the script working on a tree of boxy's but thats not what I wanted it for. So when I tryed to place it on a simple 2 tri billboard it just pops the bilboard at a strange angle and leaves it there, same thing with one of the box objects that comes with unity. Is this a bug or am I being dumb :p (Don't ask wich option I would prefer ;) ). Jeff
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The script does the billboarding only for the camera you see in the game view.

    Possibly you are looking at the billboard in the scene view? In which case it will look wrong, since it is simply looking at the game view camera which is probably placed somewhere else.
     
    adrian_Tumblr likes this.
  9. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Yup I'm looking in the game view. I'll try get some screen shots up of what it douse latter today. Jeff
     
  10. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LookAtCamera : MonoBehaviour
    5. {
    6.     public Camera cameraToLookAt;
    7.  
    8.     void Update()
    9.     {
    10.         transform.LookAt(cameraToLookAt.transform);
    11.     }
    12. }
    Thanx to Neil for helping me with this. This script of his works, you just have to select the camera you want the objects to face twards in the inspector for each object. Jeff

    P.S. If anyone wanted to modify it so that the billboard was only rotated along the Y axes I would be verry pleased.
     
    ApachyGames likes this.
  11. guategeek_legacy

    guategeek_legacy

    Joined:
    Jun 22, 2005
    Posts:
    659
    C#
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class LookAtCameraYRotationOnly : MonoBehaviour
    6. {
    7.     public Camera cameraToLookAt;
    8.  
    9.     void Update()
    10.     {
    11.         Vector3 v = cameraToLookAt.transform.position - transform.position;
    12.         v.x = v.z = 0.0f;
    13.         transform.LookAt(cameraToLookAt.transform.position - v);
    14.     }
    15. }
    16.  
    Neil's updated script. This one only rotates the object on the Y axis. So no longer will your billboards tillt up and down unwantedly :p[/code][/quote]
     
    TooManySugar likes this.
  12. cybervalie

    cybervalie

    Joined:
    Sep 27, 2009
    Posts:
    94
    Ah cool i was searching this, but how will this script look in javascript?

    I mean that an object is billboard, but only in Y axis
     
  13. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    It's very similar in JS:-
    Code (csharp):
    1. var cameraToLookAt: Camera;
    2.  
    3. function Update()
    4. {
    5.       var v: Vector3 = cameraToLookAt.transform.position - transform.position;
    6.       v.x = v.z = 0.0;
    7.       transform.LookAt(cameraToLookAt.transform.position - v);
    8. }
     
  14. bvckshot

    bvckshot

    Joined:
    May 14, 2010
    Posts:
    1
    Thank you so much for this code. Works like a charm.
     
  15. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    I tried both the C# and Jave Scripts on a GUI text, but the text shows backwards. Any way of fixing this?
     
  16. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    anyone? I'm thinking of using it for names above objects, NPC, things like that. So i need it to show correctly
     
  17. ackyth

    ackyth

    Joined:
    Oct 29, 2009
    Posts:
    146
    Why not just make a empty gameobject place the text in it (parenting it) and then rotate it 180 in Y.
     
  18. mattconley2011

    mattconley2011

    Joined:
    Feb 22, 2010
    Posts:
    105
    Thanks, works like a charm :)
     
  19. MadToLove

    MadToLove

    Joined:
    Jul 22, 2012
    Posts:
    70
    I followed the tutorial here http://www.youtube.com/watch?v=Yt6dJQpFe-s&feature=related

    And I have a plane that I made, put my fire texture on it, applied the animate uv script to it, got it sized properly, but when I try to add that JS script up there posted by andeeeee to the plane and hit play my plane flips itself and faces upwards instead of at the camera. Anyone know why this might be happening?

    I tried some of those other scripts above and when i try to put the scripts onto my plane it says that the class does not mach what is defined in the script. Im super lost.
     
  20. Alpineflame

    Alpineflame

    Joined:
    Jan 14, 2015
    Posts:
    4
    Sorry for the necrobump from hell here, but I was just writing this to add a line to the code posted, that made it work as I wished. I was having a problem where the Y axis was set at 0, and not updating, making for a not-very-satisfying billboard. Here's the code that worked for me (just add one line at 14.):

    1. using UnityEngine;
    2. using System.Collections;

    3. public class LookAtCameraYRotationOnly : MonoBehaviour
    4. {
    5. public Camera cameraToLookAt;

    6. void Update()
    7. {
    8. Vector3 v = cameraToLookAt.transform.position - transform.position;
    9. v.x = v.z = 0.0f;
    10. v.y = 90;
    11. transform.LookAt(cameraToLookAt.transform.position - v);
    12. }
    13. }
     
  21. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Hmm, so would that be considered a 2 year bump, or a 10 year bump?
     
  22. rollerstar

    rollerstar

    Joined:
    Nov 24, 2013
    Posts:
    8
    You sir, have saved this man so many headaches and struggles.
    I appreciate you.
     
  23. RealPpTheBest

    RealPpTheBest

    Joined:
    Jan 27, 2019
    Posts:
    64
    Why am I late evrywhere? Well basically it's really easy... I have made a billboard script for my slender kit..you can check it out it's called advanced slender kit. So, let's get back to the point.. so basically billboarding is just the object looking at the player camera.. so it goes something like this.

    Code (CSharp):
    1. //Script written by Pratyush Priyadarshi.
    2. //Simple script for billboarding.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6. using UnityEngine.SceneManagement;
    7. using UnityEngine.AI;
    8.  
    9. public class Billboard : MonoBehaviour {
    10.  
    11.     [Header("Player Settings")]
    12.     GameObject Player; // gameobject player
    13.     Transform player; // the Object the player is controlling
    14.    
    15.     [Header("GameObject Calculations Settings")]
    16.     public float facePlayerfactor = 20f; // angle of facing player
    17.    
    18.     void Start() {
    19.         Player = GameObject.FindWithTag("Player"); // find the gameobject with the tag of Player.
    20.         player = Player.GetComponent<Transform>(); // from the gameobject of player get the transform
    21.     }
    22.  
    23.     // The function to billboard this object to face the player
    24.     void Update()
    25.     {
    26.         Vector3 direction = (player.position - transform.position).normalized;
    27.         Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
    28.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * facePlayerfactor);
    29.     }
    30. }
    This script makes this really complex subject.. into something simple.. Hope it helps :) BTW it's C#