Search Unity

Question Transform Rotation not working

Discussion in 'Scripting' started by getmyisland_dev, Sep 10, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Before I start I want to say that I googled this question and tried out most of the answers, but nothing seemed to work for me and I apologize for asking such a stupid question, but I really have no clue right now.

    I want to rotate my player object to 0 in x axis, to 180 y axis and to 0 in z axis. I tried multiple ways of handling this, but nothing happens.

    I tried:
    - transform.Rotate
    - transform.rotation = Quaternion.Euler(0, 180, 0)
    - I tried using Space.World and Space.Self
    - Quaternion.identity
    and probably a few more I don't remember anymore.

    Is there a common issue I'm currently not aware of. Or why does this not work. Anyway every help would be much appreciated, Thank you.
     
    MMartin08 likes this.
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Post your full code (in code tags).

    If nothing happens, I'm guessing either you haven't attached the script to an object, or the function isn't being executed, which may mean it's misspelled.

    Otherwise something else may be overwriting your rotation changes with its own rotation data.
     
    MMartin08 likes this.
  3. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    Here is the code:
    Code (CSharp):
    1. public void ResetPlayer()
    2.     {
    3.         playerObject.transform.position = startPosition;
    4.         playerObject.transform.localRotation = Quaternion.identity;
    5.     }
    The position reset works like a charm, but like I said nothing happens with the rotation. Because of the position reset working I assume I assigned everything correctly.

    The position didn't work before either and I had to change the rigidbody to extrapolate in order for it to work, but I don't think that's the problem with the rotation.

    I call the code above when I press H just for Debug purposes if this helps.
     
    MMartin08 likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    You're setting .localRotation. Do you intend to set .rotation ?
     
  5. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    I just did this for test purposes, I tried out both and nothing worked. This is literally so simple, I think I'm doing something really obvious wrong.

    Can Rigidbody settings interfere with rotation or a character controller? Or like @StarManta said, how can I know if something overwrites my rotation changes.

    I'm currently using the Unity Standard assets FPS controller because I like the features with head bump and FOV change, but I'm gonna try out if there is a problem with my mouse script.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,751
    Absolutely on both counts.

    Setting a Rigidbody's rotation with ANY other method than .MoveRotation() is bypassing the physics system and all warranties are null and void. Rigidbodies also have constraints on rotation you can enable.

    CharacterControllers will also definitely set their rotation whenever you call .Move(), as you can see with this example controller script, which fixes the two-Move-call ground-test glitch in the Unity example:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746