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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotating planes only on the y axis

Discussion in 'Scripting' started by Minzkraut, Jul 4, 2014.

  1. Minzkraut

    Minzkraut

    Joined:
    May 30, 2014
    Posts:
    4
    Hi community
    What i wanna do:
    An object should always turn towards camera ... but only on the y axis ... so it won't turn up when i jump ...

    Im currently rotating it with this script:

    var Distance;
    var Target : Transform;

    function Update ()
    {
    lookAt();

    }
    function lookAt ()
    {
    var rotation = Quaternion.LookRotation(Target.position - transform.position);
    transform.rotation = rotation;
    }

    However, I do not know how I can restrict the rotation to the y axis
    Greets, Minzkraut
     
  2. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    I made a code for that some months ago, here I'll share it to you, it have the option to make the sprite only look in Y an the option for it to llok like a GUI, IT ONLY WORKS WITH QUADs or nested objects.
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var typeGui:boolean;//set this to true to face the camera always, else Y only
    4.  
    5.  
    6. function Update(){
    7.     transform.rotation=Camera.main.transform.rotation;
    8. }
    9.  
    10. function LateUpdate(){
    11. if(!typeGui){
    12.     transform.rotation.eulerAngles.x=0;
    13.     transform.rotation.eulerAngles.z=0;
    14. }  
    15. }

    Cheers!
     
    Minzkraut likes this.
  3. Minzkraut

    Minzkraut

    Joined:
    May 30, 2014
    Posts:
    4
    Thanks <3
     
  4. earrgames

    earrgames

    Joined:
    Jan 27, 2014
    Posts:
    168
    You're welcome!