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

billboard rotation around x, y axis

Discussion in 'Scripting' started by michaelvogt, Jun 20, 2019.

  1. michaelvogt

    michaelvogt

    Joined:
    Jun 12, 2019
    Posts:
    3
    Hello everyone.

    I need to create a billboard effect, so an object always faces the camera. Found this code to do so:

    Code (CSharp):
    1. transform.LookAt(transform.position + m_Camera.transform.rotation * Vector3.forward);
    Works in principle. Problem is, that the gameobject also follows when the camera is rotated around the z-axis, which I fail to find a way to prevent. Looks like I'm missing something.

    upload_2019-6-20_14-46-9.png

    The sign should not be rotated relative to the pole.

    Any tips are welcome.


    Thanks,
    Michael
     
  2. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    you need to take the z axis out of the equation.

    Code (CSharp):
    1. Vector3 targetPos = new Vector3(m_camera.transform.position.x, m_camera.transform.position.y, transform.position.z);
    2.             transform.LookAt(targetPos);
     
  3. michaelvogt

    michaelvogt

    Joined:
    Jun 12, 2019
    Posts:
    3
    Thank you for your answer.
    While it didn't solve the problem, I realized that I did look at the wrong place to find the problem.
    It turned out that I set up my model wrong. Fixing this, my original code started working.

    Thanks a lot
     
  4. Team2Studio

    Team2Studio

    Joined:
    Sep 23, 2015
    Posts:
    98
    no problem, I'm glad you found a solution :)