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

Question Why does the rotation change?

Discussion in 'Scripting' started by Slurgelman, Nov 15, 2022.

  1. Slurgelman

    Slurgelman

    Joined:
    Aug 25, 2022
    Posts:
    7
    If I use this code:
    Code (CSharp):
    1. weaponDrawn = false;
    2.  
    3. private void Update()
    4.     {
    5.         if (!weaponDrawn)
    6.         {
    7.             DrawingWeapon();
    8.         }
    9.     }
    10.  
    11. private void DrawingWeapon()
    12.     {
    13.         if (Input.GetKeyDown("r"))
    14.         {
    15.             Instantiate(weapon, new Vector3(transform.position.x + 0.45f, transform.position.y + 0.12f, 0), Quaternion.Euler(0, 0, 0), rightHand.transform);
    16.             weaponDrawn = true;
    17.         }
    18.     }
    It doesn't have the wrong rotation, but if I use this code:

    Code (CSharp):
    1. weaponDrawn = false;
    2.  
    3. private void Update()
    4.     {
    5.         if (!weaponDrawn)
    6.         {
    7.             DrawingWeapon();
    8.         }
    9.     }
    10. private void DrawingWeapon()
    11.     {
    12.         Instantiate(weapon, new Vector3(transform.position.x + 0.45f, transform.position.y + 0.12f, 0), Quaternion.Euler(0, 0, 0), rightHand.transform);
    13.         weaponDrawn = true;
    14.     }
    The instantiated game object is rotated 45(ish) degrees.

    Why does this happen?
     
  2. Slurgelman

    Slurgelman

    Joined:
    Aug 25, 2022
    Posts:
    7
    Update:

    For some reason, if I wait a little bit, the second code doesn't have any rotation.

    Why does it change depending when the code is activated?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    No human being should ever have to deal with a line of code like that.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums

    As to the actual problem, all about Euler angles and rotations, by StarManta:

    https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html
     
    Slurgelman likes this.