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. Dismiss Notice

changing obj angle and resetting it

Discussion in 'Scripting' started by w4ssup, Nov 23, 2016.

  1. w4ssup

    w4ssup

    Joined:
    Feb 16, 2016
    Posts:
    34
    I have a children transform rotation set to (0,0,0) at start, I want to change it to (x,0,0) after I down a button, and back to (0,0,0) when I release it.

    I did transform.Rotate(x,0,0); and it works, but transform.Rotate(0,0,0) doesn't work for resetting it, it just keep adding up the x for some reason. How do I reset to it back to it default rotation? (note I'm only changing the x rotation relative to the obj location
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    transform.Rotate, how to say, is kind of a verb. It does something. I mean this function ROTATE something on a certain amount of degrees. When you say Rotate 0, nothing just happens, because it tries to rotate the object on 0 degrees. If you want to set up a specific rotation, then you should use:

    Code (CSharp):
    1. transform.rotation = new Quaternion.Euler(x, 0, 0);

    There should be an error, because it should not keep adding up the x, but adding just nothing. Thus, you have somewhere an error. I would suggest that it is in the IF statement, where you check the keyboard.
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    I think he means it just rotating every time the button is pressed. As you pointed out rotate by 0 does nothing, so each press is doing +x.
     
  4. w4ssup

    w4ssup

    Joined:
    Feb 16, 2016
    Posts:
    34
    Getting an error with adding "new" before quaternion. I just want to reset it back to 0 after the function (since it increase before the function
     
  5. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    Oops! I am sorry, my bad. "New" is not needed in this case, indeed.

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0, 0, 0); // Reset rotation to 0
    Btw, you also can Rotate it back with -x instead.

    Code (CSharp):
    1. transform.Rotate(-x,0,0);
     
  6. w4ssup

    w4ssup

    Joined:
    Feb 16, 2016
    Posts:
    34
    for some reason, my y rotation keep on changing even tho when I dont want it to change
    set angle
    Code (csharp):
    1. m_FireTransform.rotation = Quaternion.Euler(-shootAngle, 0,0);
    reset
    Code (csharp):
    1. m_FireTransform.rotation = Quaternion.Euler(0, 0, 0);
     
  7. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    Show the script. Probably the problem is somewhere there.
     
  8. w4ssup

    w4ssup

    Joined:
    Feb 16, 2016
    Posts:
    34
    edit: nvm fix it, all I had to do was change the angle at which velocity was added to the rigid body
     
    Last edited: Nov 25, 2016