Search Unity

90 degrees turn of an object? c#

Discussion in 'Scripting' started by Alex Cruba, Aug 23, 2013.

  1. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I just need to change the y axis, but

    Code (csharp):
    1. transform.rotation.y = 90;
    doesn't work.
     
  2. Jallen182

    Jallen182

    Joined:
    Aug 21, 2012
    Posts:
    97
    You can't access the individual rotations, they're read-only. There are a few different options for rotating a GameObject. One would be using Transform.Rotate, but the easiest way illustrate how to rotate a GameObject would be:

    Code (csharp):
    1. // Sets the transform's eulerAngles (rotations represented as degrees) to the desired
    2. // rotation on the Y axis without changing the X or Z axes.
    3. float yRotation = 90.0f;
    4. transform.eulerAngles = new Vector3(transform.eulerAngles.x, yRotation, transform.eulerAngles.z);
     
    ChonkyPixel and lexen1 like this.
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That's not true; why do you think it? It's just unlikely that you'll want to, often. I've only done it for 2D rotations.

    Rotation is a quaternion. Setting its .y to 90 is meaningless; Unity won't even tolerate that value.
     
    Last edited: Aug 23, 2013
  4. dogzerx2

    dogzerx2

    Joined:
    Dec 27, 2009
    Posts:
    3,971
    This is the first thing I tried when I downloaded unity, to rotate stuff... and the whole time I thought the rotation was a Vector3, but yeah... it's a Quaternion... (even though I still have no idea what a Quaternion is, only smart ppl know the details)

    as Jallen182 said use eulerAngles instead, (or localEulerAngles).

    But some recommend using the Rotate() functions. This one time I wanted to do this quick flight simulator... and I've got this issue where my plane would get stuck on the Y axis when increasing eulerAngles manually. But using Rotate() function didn't give me this problem.
     
  5. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    At last I got the solution without rotating the cube... My rotation command was wrong. It works like I planned it - like steering a car with hold down up arrow and (cars can't but owners would love it) left and right strafe when up arrow is not hold down. Thank you all...
     
  6. Jallen182

    Jallen182

    Joined:
    Aug 21, 2012
    Posts:
    97
    Any time I try to modify one of the axes on rotation or position I get this error:

    "Cannot modify the return value of 'UnityEngine.Transform.rotation' because it is not a variable"

    This thread talks a lot about how position/rotation are used in Transform: 'Cannot modify .... because it is not a variable' in C#.
     
  7. MikHaven

    MikHaven

    Joined:
    Jan 13, 2015
    Posts:
    7
    THANK you very MUCH Jallen,

    We ask a question and people gives us bizarre answers, like
    rotation *= Quaternion.Euler(90, 0, 0)
    which rotates it relative to all the other angles, when humans just say I want to rotate it by a angle, which your code does perfectly. I guess the problem is when we asked for the rotation it is in bizarre numbers but we need to get the Angles. Which is dumb because setting the number in the inspector to 90 works, because its in Eulor, while we ask for the number we get it in another format.

    The full code line I am using is:
    canvas.transform.eulerAngles =newVector3(90f, Camera.main.transform.eulerAngles.y, Camera.main.transform.eulerAngles.z);

    I have farms on the ground and want to display text on them that rotates with my characters camera. So I need them to be on the ground level and not in the air like my trees.
     
    Last edited: Feb 15, 2015
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    MikHaven, you should ask more out of yourself. Everything is "bizarre" until you understand it.
     
    DoomDude99 and unity_1sp12cs067 like this.
  9. 00yoshi

    00yoshi

    Joined:
    Jan 24, 2015
    Posts:
    16
    Transform.eulerAngles.y = 90;
     
    DoomDude99 likes this.
  10. KnightsHouseGames

    KnightsHouseGames

    Joined:
    Jun 25, 2015
    Posts:
    850
    I'm trying a solution like this for my game for my character, but I find that it can't be iterated. Once the EularAngle gets above 360 or below zero, the system freaks out and starts spinning out of control. When I try to reset it to zero when it goes above 360, it rotates back around the wrong way rather than just staying put. And then when I try rotating left, all hell breaks loose and it just rotates in any old direction

    My game requires very precise, smooth rotation in 90 degree increments. How would I handle that?

    Edit: Belay that, I found the answer. Quaternion.Eular saved the day.

    I converted the system over to quaternions, but I used Quaternion.Eular in my actual calculation to save myself some trouble.
     
    Last edited: Jun 29, 2015
  11. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    psudo code because...

    if rotation > 360, rotation -= 360;