Search Unity

Bug FIXED: Inconsistent object rotation after pressing "R" key

Discussion in 'Scripting' started by Hyppetrain, Oct 13, 2021.

  1. Hyppetrain

    Hyppetrain

    Joined:
    Oct 2, 2021
    Posts:
    10
    Hello, so I'm building my first RTS game (cause I love them) and I have a super-simple code for rotating a building before placing it. However sometimes (quite often) when I press "R", the building does not rotate and when I press the button again it rotates twice (it rotates double the ammount it should). Help would be highly appreciated.

    My code:
    Code (CSharp):
    1. private int rotationAngle = 0;
    2.  
    3. if(Input.GetKeyDown("r"))
    4.             {
    5.                 rotationAngle += 90;
    6.            
    7.                 currentBlueprint.transform.rotation = Quaternion.Euler(0, rotationAngle, 0);
    8.  
    9.                 currentBuilding.transform.rotation = Quaternion.Euler(0, rotationAngle, 0);
    10.        
    11.             }
    ok looks like I fixed it. I made a simple and weird change, no idea why its having an effect, but Im not complaining.

    Code (CSharp):
    1. public int rotationAngle = 0;
    2. public int buildingAngle;
    3.  
    4. if(Input.GetKeyDown("r"))
    5.             {
    6.                 rotationAngle += 90;
    7.                 buildingAngle = rotationAngle;
    8.              
    9.                 currentBlueprint.transform.rotation = Quaternion.Euler(0, buildingAngle, 0);
    10.                 currentBuilding.transform.rotation = Quaternion.Euler(0, buildingAngle, 0);
    11.          
    12.             }
    (I made the ints public just so I could watch them in the editor)
     
    Last edited: Oct 14, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    Make sure the code above only runs from being called in Update(), not FixedUpdate() or anything else.
     
    Hyppetrain likes this.
  3. Hyppetrain

    Hyppetrain

    Joined:
    Oct 2, 2021
    Posts:
    10
    Yep, it's running in Update() (and has been the whole time), but is not working.
    thanks for replying!
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I suspect something else is sometimes setting the rotation. So like:
    start at rotation 0
    hit R, "rotationAngle" set to 90, transform rotation set to 90
    But immediately, another function or script sets transform rotation back to 0
    hit R again, "rotationAngle" set to 180, the other script is no longer changing transform rotation, so it gets set to 180
     
    Hyppetrain likes this.
  5. Hyppetrain

    Hyppetrain

    Joined:
    Oct 2, 2021
    Posts:
    10
    Ok ok I will investigate
     
  6. ceceomer

    ceceomer

    Joined:
    Sep 1, 2014
    Posts:
    24
    It is race condition problem. Don't declare private int rotationAngle = 0; under Update method. Declare it under class.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    OP has clearly pulled different lines from different parts of the code. If they had line 1 inside Update, they'd be getting a compiler error. Also, that wouldn't be anything remotely like a race condition.
     
  8. Hyppetrain

    Hyppetrain

    Joined:
    Oct 2, 2021
    Posts:
    10
    the int RotationAngle is declared at the top of the code and in Update() I have a RotateWhenClicked (or something like that) method that is defined below.
     
  9. Hyppetrain

    Hyppetrain

    Joined:
    Oct 2, 2021
    Posts:
    10
    the int RotationAngle is declared at the top of the code and in Update() I have a RotateWhenClicked (or something like that) method that is defined below. so yeah you right
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    @StarManta always right... he dat gud.