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

How to Get and set rotation question

Discussion in 'Scripting' started by GoodNight9, Dec 28, 2015.

  1. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Hello Everyone!

    I have a question regarding rotation. I have an object parented to another object. Upon a trigger, I want it to rotate on the 'y' axis until the y axis = 90. When the trigger is no longer active. I want it to rotate back on the y axis until y axis = 0. But I want it to do that without changing the 'x' or 'z' axis that it should be following based on the parent.

    transform.rotate seems to always make the object rotate by a certain amount, not to a specific limit. I've been stumped by this for a while. Thank you very much for your time!
     
  2. elelec

    elelec

    Joined:
    Jul 21, 2014
    Posts:
    7
    I'm assuming you are using eulerAngles for the rotation, and in this case, what you can do is something like this:
    Code (CSharp):
    1. transform.eulerAngles = new Vector3(transform.eulerAngles.x, desiredYAngle, transform.eulerAngles.z);
    Might be a bit boring to write, but that should let you set the angle to whatever you want it to be, in any direction.

    Also, the object is the child of another object and you wanna change the local rotation, use localEulerAngles
     
    GoodNight9 likes this.
  3. GoodNight9

    GoodNight9

    Joined:
    Dec 29, 2013
    Posts:
    123
    Thank you very much!