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

Question Billboarding sprite, rotating on a separate axis

Discussion in 'Scripting' started by rabbitish, Sep 19, 2022.

  1. rabbitish

    rabbitish

    Joined:
    Mar 9, 2014
    Posts:
    4
    I'm doing a 2.5D game with flat sprites that billboard to face the camera, rotating on the Y axis. I also want them to continuously rotate on the Z axis for a smooth spinning visual.

    I don't have a great understanding of Unity's transform and rotation and can't get these two ideas working at the same time together.

    My sprites have this to manage billboarding:

    Code (CSharp):
    1. transform.rotation = camera.transform.rotation;
    2. transform.rotation = Quaternion.Euler(0f, transform.rotation.eulerAngles.y, 0);
    And hypothetically this works to rotate a sprite, but it's not working together with the billboarding idea.

    Code (CSharp):
    1. transform.Rotate(0, 0, speed * Time.deltaTime);
    Any help is greatly appreciated, thank you.
     
  2. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Try to += Vector3(x,y,z) to the Euler angles.

    or rather

    += Sprite.transform.right to the Euler angles.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Make a short hierarchy of Transforms parented together, something like:

    MyBasePrefab
    MySpinningOnZTransform
    TheActuaSprite


    Only do the "billboard" the MyBasePrefab object (eg, rotate it around Y axis to face camera)

    Always spin the MySpinningOnZTransform by setting its .localRotation:

    Code (csharp):
    1. MySpinningOnZTransform.localRotation = Quaternion.Euler( 0, 0, Time.time * 200);
     
    rabbitish likes this.
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    I thought further about this one after I had posted and he could make an int that climbs from 0-360 maybe, possibly, and then he could have his transforms forward = camera transform forward + ((vector axis) * Int degree)
     
    rabbitish likes this.
  5. rabbitish

    rabbitish

    Joined:
    Mar 9, 2014
    Posts:
    4
    Thanks so much for both of your suggestions - ended up going with the Transforms solution. Visualizing things in the Hierarchy window is a lot more clear to me, and this worked perfectly.