Search Unity

Start Y Rotation vs Current Y Rotation Readout?

Discussion in 'Scripting' started by NeatMaddness, Oct 19, 2017.

  1. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    First off thank you for the help, what I am trying to do is fairly straightforward. I want to get my initial Y rotation (-90) and compare it to my current Y rotation.

    Code (CSharp):
    1.  float yRotation = this.gameObject.transform.localEulerAngles.y;
    2. currentCameraRotationY =  yRotation;
    localEulerAngles does exactly what I am looking for, records players current rotation, goes to 0 once it passes 360 degrees, outputs to a float, and everything. The problem is my character's start rotation is 270 degrees (-90) and it needs to remain that. I am displaying the degrees on screen and I would like the display to be 0 degrees when the level starts regardless of what its actual starting rotation is (-90).

    Is there a way to make the eulerAngles start at 0 degrees even when my start is 270 degrees?

    I have also been looking into Quaternions but I am not a great programmer and they are pretty confusing to get working properly.

    Thank you again for your help!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You could subtract 90 from what would otherwise be displayed on the screen?

    You could make the game object a child of another, adjust its rotation so it's what you want, then use the parent object for your scripts, perhaps. It could have 0 while the child has -90, for instance.
     
  3. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Thanks for the response, I have tried both those things. Subtracting 90 from it makes the angles mess up, unless I am doing it wrong. When I do it, it makes 270 degrees as far as it will go. It doesnt go up to 360 anymore.

    Code (CSharp):
    1.  float yRotation = this.gameObject.transform.localEulerAngles.y;
    2.         currentCameraRotationY =  yRotation + 90;
    This is what I was trying.

    I have gotten the Quaternions almost doing what I want but it resets after 180. This is what I currently have for those.

    Code (CSharp):
    1. public Transform target;
    2. public Quaternion originalRotationValue;
    3.  
    4.  
    5. void Start()
    6.     {
    7.         originalRotationValue = transform.rotation;
    8.         target = this.gameObject.transform;
    9.     }
    10.  
    11. void Update()
    12.     {
    13.         float angle = Quaternion.Angle(originalRotationValue, target.rotation);
    14.         currentCameraRotationY = angle;
    15.     }
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    As methos said, use a parent object to act as your base rotation if you need to. But really, this is something you could simply fix in your modeling software. You need to export in a different world space axis so that your starting 0,0,0 rotation is the one you actually want.


    It should go up to 360 still... In what context is it not going up to 360?
     
  5. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Sorry, I meant it goes up to 450 now which is where it resets. Here is the screen output, when I load in it starts at 360 degrees (obviously the 270 + 90) and when I rotate it goes up to 450 (360 + 90). The left number is my Y rotation and the right number is my X rotation (which is already working properly)

     
  6. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Honestly, I think I am just going to live with this Quaternion script I have working where is compares the originalRotationValue to the current. It only goes up to 180 but I think I can live with that and what I need it for.

    Thanks for the help
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Agreed, I should have mentioned the modelling software change. That's a little personal bias as I have little experience with that, and think other people might be in the same boat ;)

    I see your response here, as I was replying..
    Pretty sure for Quaternion, this might work:
    Code (csharp):
    1. float adjustedAngle = (transform.localRotation * Quaternion.Euler(0, 90,0)).localEulerAngles.y;
    2.  
    Or you could do the parenting thing, the adjuting in software thing, or just work out the math yourself.
    float angle = eulerAngles.y % 360; // I think would work.
    or like : if angle > 360 angle -= 360; :)
     
    NeatMaddness likes this.
  8. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    ^Was just about to write that damnit haha, yeah the modulo is the simplest way, so the number will always wrap around the 0-360 range by returning the remainder of the modulo operation.
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    heh.. I edited my post a little from -90 to 90 but anyways.. something in this list has got to be helpful :)
     
  10. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30

    Its giving me an error saying " 'Quaternion' does not contain a definition for 'localEulerAngles' "

    I will look into the export problem from my modeling software.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry about that.. I believe it's just eulerAngles. :) Should already be 'local' because of the multiplication**
     
  12. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    I got it! It doesnt work with localEulerAngles but it does with just straight eulerAngles!

    Thank you so much!!
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, cool glad ya got it :)
     
  14. NeatMaddness

    NeatMaddness

    Joined:
    Nov 14, 2014
    Posts:
    30
    Works exactly as I wanted. I will still look into that modeling software problem so I don't run into trouble like this again. Thank you for your time!
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I was just about to say that ! lol

    For 1 model, it's not a huge deal.. for many, sigh. .that would suck ;)
     
    NeatMaddness likes this.