Search Unity

Question My float angle isn't changing!

Discussion in 'Scripting' started by uotsabchakma, Jul 27, 2020.

  1. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Here's half of my code first:
    Code (CSharp):
    1. if (Input.GetKeyDown("left"))
    2. {
    3. rotating.z = 1;
    4. }
    5. if (Input.GetKeyDown("right"))
    6. {
    7. rotating.z = -1;
    8. }
    9. if (Input.GetKeyUp("left"))
    10. {
    11. rotating = new Vector3(0, 0, 0);
    12. rzlerp = "toright";
    13. angle.z = transform.localEulerAngles.z;
    14. }
    15. if (Input.GetKeyUp("right"))
    16. {
    17. rotating = new Vector3(0, 0, 0);
    18. rzlerp = "toleft";
    19. angle.z = transform.localEulerAngles.z;
    20. }
    21. if (rzlerp == "toright")
    22. {
    23. angle.z *= 0.9f;
    24. transform.eulerAngles = angle;
    25. if (angle.z < 5)
    26. {
    27. rzlerp = "null";
    28. }
    29. }
    30. if (rzlerp == "toleft")
    31. {
    32. angle.z *= 0.9f;
    33. transform.eulerAngles = angle;
    34. if (-5 < angle.z)
    35. {
    36. rzlerp = "null";
    37. }
    38. }
    39. }
    40. }
    I saw in the inspector when I release left arrow key, the angle of course discreases. But when I release right arrow key the angle suddenly changes to more biggere number than 200! And also the rzlerp get's the string "null". But I think when I release the right arrow key, the rzlerp should be "toleft" and angle.z should be discreased. Why rzlerp isn't turning to "toleft" and angle.z get's bigger number when I release the right arrow key? If you can solve the problem so please help me to solve it.
    If you'll need the full code so ask me. I have no problem to share the full code.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    If you are linearly changing an angular variable, you really don't want to do it via the .eulerAngles property. The reason is, these are not guaranteed to be what you think they are. Quaternions do NOT contain eulerAngles inside them. When you use that property it is synthesized to make it useful in the editor.

    Instead keep your own separate floating point variable representing that angle, process your player inputs to change that angle and bounds-check it, then create a fresh Quaternion and assign it to the rotation like si:

    Code (csharp):
    1. transform.rotation = Quaternion.Euler( 0, 0, myAngle);
    That way you never read or care about the .eulerAngles field.
     
    uotsabchakma likes this.
  3. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Not working yet. I think I didn't explained the problem well yet. Here's my full code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public GameObject playerFire = null;
    8.     public GameObject playerMissile = null;
    9.     public GameObject playerBomb;
    10.     public GameObject playerFireSmoke;
    11.     public Vector3 rotating;
    12.     public Vector3 angle;
    13.     public float movementSpeed = 1f;
    14.     public float horizontalInput;
    15.     public float verticalInput;
    16.     public float health = 10;
    17.     public float missiles = 7;
    18.     public float bombs = 3;
    19.     public bool MLchangable = true;
    20.     public bool MRchangable = false;
    21.     public string side = "left";
    22.     public string rzlerp;
    23.     void Update()
    24.     {
    25.         var pos = transform.position;
    26.         transform.eulerAngles += rotating;
    27.         pos.x = Mathf.Clamp(pos.x, -38f, 38f);
    28.         pos.z = Mathf.Clamp(pos.z,-3.5f, 44.8f);
    29.         transform.position = pos;
    30.         health = Mathf.Clamp(health, 0, 10);
    31.         missiles = Mathf.Clamp(missiles, 0, 10);
    32.         bombs = Mathf.Clamp(bombs, 0, 10);
    33.         if (Input.GetKeyDown("m"))
    34.         {
    35.             if (missiles > 0)
    36.             {
    37.                 Instantiate(playerMissile, transform.position, Quaternion.identity);
    38.                 missiles -= 1;
    39.                 if (MLchangable == true & side == "left")
    40.                 {
    41.                     MLchangable = false;
    42.                 }
    43.                 if (MRchangable == true & side == "right")
    44.                 {
    45.                     MRchangable = false;
    46.                 }
    47.             }
    48.         }
    49.         if (Input.GetKeyUp("m"))
    50.         {
    51.             if (MLchangable == false & side == "left")
    52.             {
    53.                 MRchangable = true;
    54.                 side = "right";
    55.             }
    56.             if (MRchangable == false & side == "right")
    57.             {
    58.                 MLchangable = true;
    59.                 side = "left";
    60.             }
    61.         }
    62.         if (Input.GetKeyDown("space"))
    63.         {
    64.             Instantiate(playerFire, transform.position, Quaternion.identity);
    65.             Instantiate(playerFireSmoke, transform.position, Quaternion.identity);
    66.         }
    67.         if (Input.GetKeyDown("b") & bombs > 0)
    68.         {
    69.             Instantiate(playerBomb, transform.position, Quaternion.identity);
    70.             bombs -= 1;
    71.         }
    72.         horizontalInput = Input.GetAxis("Horizontal");
    73.         verticalInput = Input.GetAxis("Vertical");
    74.         transform.position += new Vector3(horizontalInput * movementSpeed, 0f, verticalInput * movementSpeed);
    75.         if (Input.GetKeyDown("left"))
    76.         {
    77.             rotating.z = 1;
    78.         }
    79.         if (Input.GetKeyDown("right"))
    80.         {
    81.             rotating.z = -1;
    82.         }
    83.         if (Input.GetKeyUp("left"))
    84.         {
    85.             rotating = new Vector3(0, 0, 0);
    86.             rzlerp = "toright";
    87.             angle.z = transform.localEulerAngles.z;
    88.         }
    89.         if (Input.GetKeyUp("right"))
    90.         {
    91.             rotating = new Vector3(0, 0, 0);
    92.             rzlerp = "toleft";
    93.             angle.z = transform.localEulerAngles.z;
    94.         }
    95.         if (rzlerp == "toright")
    96.         {
    97.             angle.z *= 0.9f;
    98.             transform.rotation = Quaternion.Euler(0, 0, angle.z);
    99.             if (angle.z < 5)
    100.             {
    101.                 rzlerp = "null";
    102.             }
    103.         }
    104.         if (rzlerp == "toleft")
    105.         {
    106.          
    107.             angle.z *= 0.9f;
    108.             transform.rotation = Quaternion.Euler(0, 0, angle.z);
    109.             if (-5 < angle.z)
    110.             {
    111.                 rzlerp = "null";
    112.             }
    113.         }
    114.     }
    115. }
    And the problem is when I press left, the player fairly turns it's z rotation axis to left (gets bigger number than 90). And when I press the right arrow key, it turns right greatly but when I release right arrow button, it suddenly turns too much right side. I saw in the inspector that the angle get changed to more bigger number than 200 when I release the right arrow key. I can't yet understand why is this problem. If this is my problem so which line has this problem? If you know why it's not working properly so please tell me!
    (Also I don't know that dose Unity forum supports video files, I actually want to upload a .wmv video to saw you guys my problem.)
    (Is this is a Unity bug or my problem?)
     

    Attached Files:

  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    This problem is because of the first line I wrote above. You cannot store any number you want in eulerAngles and expect to get it back. It doesn't work that way.

    Let me know when you try what I suggested with your own float (myAngle) that you adjust and then set.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Kurt-Dekker likes this.
  6. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    I actually didn't understood your last words, because I can't read hard English words correctly sorry. Are you saying that you have suggested a code for me and asking me if I want that code? If that's you're saying so yes, I want. I'll understand any code you'll give me. Just write the code and I'll know how to apply it to my code. If you didn't understood problem I can't solve so you can ask me. You can ask me for any information about my game. I can give it to you. Just please let me know how to solve this thing out.
     
  7. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The problem is reading Euler angles from a rotation. You can assign to transform.eulerAngles all day every day and won't run into problems, but when you read that value out is where the problem comes in. On line 26 you have += which is shorthand for:
    Code (csharp):
    1.         transform.eulerAngles = transform.eulerAngles + rotating;
    Which means it reads the Euler angles, adds to them, then assigns them back. Reading the Euler angles is the problematic part.

    Track your own Vector3 variable for current rotation, use += on this, and then assign .eulerAngles to that variable.
    Code (csharp):
    1. Vector3 myRotation = Vector3.zero;
    2.  
    3. void Update {
    4. ...
    5. myRotation += rotating;
    6. transform.eulerAngles = myRotation;
    7. }
     
    uotsabchakma likes this.
  9. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Ok I understood. Looks like I'll need to change some things to follow that way. I'll tell you guys about if the code is working. If I then also get problems and can't solve, so you guys again will need to help me. Thanks for sawing me the example. That maked me clear about using of eularAngles!
     
  10. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Thanks, now that problem solved. But now another problem. This should be a my problem as I'm new to Unity codings. First you should see my new code-
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public GameObject playerFire = null;
    9.     public GameObject playerMissile = null;
    10.     public GameObject playerBomb;
    11.     public GameObject playerFireSmoke;
    12.     public Vector3 rotating;
    13.     public Vector3 Rotation = new Vector3(0, 0, 0);
    14.     public float movementSpeed = 1f;
    15.     public float horizontalInput;
    16.     public float verticalInput;
    17.     public float health = 10;
    18.     public float missiles = 7;
    19.     public float bombs = 3;
    20.     public float angle_z;
    21.     public bool MLchangable = true;
    22.     public bool MRchangable = false;
    23.     public string side = "left";
    24.     public string rzlerp;
    25.     void Update()
    26.     {
    27.         var pos = transform.position;
    28.         Rotation += rotating;
    29.         transform.eulerAngles = Rotation;
    30.         pos.x = Mathf.Clamp(pos.x, -38f, 38f);
    31.         pos.z = Mathf.Clamp(pos.z,-3.5f, 44.8f);
    32.         transform.position = pos;
    33.         health = Mathf.Clamp(health, 0, 10);
    34.         missiles = Mathf.Clamp(missiles, 0, 10);
    35.         bombs = Mathf.Clamp(bombs, 0, 10);
    36.         if (Input.GetKeyDown("m"))
    37.         {
    38.             if (missiles > 0)
    39.             {
    40.                 Instantiate(playerMissile, transform.position, Quaternion.identity);
    41.                 missiles -= 1;
    42.                 if (MLchangable == true & side == "left")
    43.                 {
    44.                     MLchangable = false;
    45.                 }
    46.                 if (MRchangable == true & side == "right")
    47.                 {
    48.                     MRchangable = false;
    49.                 }
    50.             }
    51.         }
    52.         if (Input.GetKeyUp("m"))
    53.         {
    54.             if (MLchangable == false && side == "left")
    55.             {
    56.                 MRchangable = true;
    57.                 side = "right";
    58.             }
    59.             if (MRchangable == false && side == "right")
    60.             {
    61.                 MLchangable = true;
    62.                 side = "left";
    63.             }
    64.         }
    65.         if (Input.GetKeyDown("space"))
    66.         {
    67.             Instantiate(playerFire, transform.position, Quaternion.identity);
    68.             Instantiate(playerFireSmoke, transform.position, Quaternion.identity);
    69.         }
    70.         if (Input.GetKeyDown("b") & bombs > 0)
    71.         {
    72.             Instantiate(playerBomb, transform.position, Quaternion.identity);
    73.             bombs -= 1;
    74.         }
    75.         horizontalInput = Input.GetAxis("Horizontal");
    76.         verticalInput = Input.GetAxis("Vertical");
    77.         transform.position += new Vector3(horizontalInput * movementSpeed, 0f, verticalInput * movementSpeed);
    78.         if (Input.GetKeyDown("left"))
    79.         {
    80.             rotating.z = 1;
    81.         }
    82.         if (Input.GetKeyDown("right"))
    83.         {
    84.             rotating.z = -1;
    85.         }
    86.         if (Input.GetKeyUp("left"))
    87.         {            
    88.             angle_z = transform.localEulerAngles.z;
    89.             Rotation = new Vector3(0, 0, 0);
    90.             rotating = new Vector3(0, 0, 0);
    91.             rzlerp = "toright";
    92.         }
    93.         if (Input.GetKeyUp("right"))
    94.         {
    95.             angle_z = transform.localEulerAngles.z;
    96.             Rotation = new Vector3(0, 0, 0);
    97.             rotating = new Vector3(0, 0, 0);
    98.             rzlerp = "toleft";
    99.         }
    100.         if (rzlerp == "toright")
    101.         {
    102.             angle_z *= 0.9f;
    103.             transform.rotation = Quaternion.Euler(0, 0, angle_z);
    104.             if (angle_z < 5f)
    105.             {
    106.                 rzlerp = "null";
    107.             }
    108.         }
    109.         if (rzlerp == "toleft")
    110.         {
    111.             angle_z *= 0.9f;
    112.             transform.rotation = Quaternion.Euler(0, 0, angle_z);
    113.             if (angle_z < -5f)
    114.             {
    115.                 rzlerp = "null";
    116.             }
    117.         }
    118.     }
    119. }
    120.  
    With this code you know when I'll release left arrow key after holding that key, the rzlerp string should be "toright". And because of 'if (rzlerp == "toright")' player ship should turn left until rzlerp is "null". That happens, but when I release the right arrow key after holding it down, the rzlerp should be "toleft" right? But what's happening is the rzlerp is "null" even when I release the right arrow key. I think it's because of line 112 to 115, which could be getting true and making the rzlerp "null". rzlerp should be "toleft" when I release the right arrow key. When I delete from line 112 to 115, the rzlerp get's "toleft" but damnly angle_z turns to positive(+) number, but not negative(-) number. So it rotates too much and gets lesser. Oh, I also just understood now why angle_z is getting null, during writing. So that means angle_z, as this is a positive number, if of line 112 getting true and rzlerp getting "null". Now my question is why angle_z isn't getting a negative(-) number? Why it's suddenly moving to a big positive number? Please say which line is the problem and how the code of that line would be.
     
    Last edited: Jul 28, 2020
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You've added new lines (88, 95) that have the same problem: you read the value of transform.eulerAngles (or transform.localEulerAngles, which is the same deal). You need to not do that at all. You need to track your rotation entirely within this script, and only ever set eulerAngles, never get it.
     
    Kurt-Dekker likes this.
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    This is getting (reading) eulerAngles:

    Code (csharp):
    1. angle_z = transform.localEulerAngles.z;
    DO NOT DO THAT! IT WILL NOT WORK!

    This is setting eulerAngles... do it this way:

    Code (csharp):
    1. transform.rotation = Quaternion.Euler( x, y, z);
     
    uotsabchakma likes this.
  13. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Is there another way to get the rotation of the object to angle_z? I actually don't know what's there without eularAngle. Please guys saw me how can I write line 88 and 95!
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    See my first comment, I gave you the answer with a code sample: Keep track of the rotation yourself with your own Vector3, and then just completely overwrite transform.rotation with that (as Euler angles).
     
    uotsabchakma likes this.
  15. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Okay guys, after many changes my code is working! Thanks to you guys for helping me. I needed just a little change to my code. Only coding my player tooked some days to be successful. My code is now working, hurrah!:)
     
    Kurt-Dekker likes this.