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. Dismiss Notice

Bug where disabled euler angle print out causes spastic behavior

Discussion in 'Scripting' started by Braineeee, Jan 10, 2016.

  1. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    I'm not sure why this is happening (its a bug, so duh) but when I print the value of eulerAngles retrieved from an object my game operates correctly. Yet when those print statements are disabled via commenting them out, my game does not operate correctly. I have video to prove and describe the problem:



    I'm sure the problem has something to do with the print statements because as you saw in the video, this only happens when I disable them, and that's the only thing I changed.

    EDIT: I just found the source of it. Ive added a comment to the line responsible. Just enable it, leaving all other print statements disabled and the problem manifests.

    The code I am using:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SwivelGuns : MonoBehaviour
    6. {
    7.     static List<GameObject> barrels = new List<GameObject>(); //Refers to barrel objects
    8.     static List<GameObject> bases = new List<GameObject>(); //Refers to base objects
    9.     static List<GameObject> bulletSpawns = new List<GameObject>(); //Refers to barrel bullet spawn points
    10.     static List<GameObject> gunLookers = new List<GameObject>();
    11.     public float xMin, xMax, yMin, yMax;
    12.     public float angleDelta;
    13.     Gun[] gunScript;
    14.  
    15.     // Use this for initialization
    16.     void Awake()
    17.     {
    18.         bases = GunController.getChildObjectsWithTag(gameObject, "GunBase");
    19.         barrels = GunController.getChildObjectsWithTag(gameObject, "GunBarrel");
    20.         bulletSpawns = GunController.getChildObjectsWithTag(gameObject, "BulletSpawn");
    21.         gunScript = gameObject.GetComponentsInChildren<Gun>();
    22.         gunLookers = GunController.getChildObjectsWithTag(gameObject, "GunLooker");
    23.     }
    24.  
    25.     public static float clampAngle (float ang, float min, float max)
    26.     { //Clamps the angle to the specified range! Works perfectly!
    27.         float n = Mathf.Round(Mathf.Abs(ang / 360f)); //Find how ang goes in to 360
    28.        // print("N = " + n);   <<<< This is the problem line I discovered. When commented out, the problem occurs, when enabled it does not.
    29.         ang = Mathf.Abs(ang);
    30.    
    31.         if(ang >= 360f)  //If above 360
    32.         {
    33.             ang -= (360f * n); //Force the value down to less than 360
    34.             //print("Angle was multiple of 360.");
    35.         }
    36.  
    37.         if (ang < max && ang > min) //If in outside the range of the min and max
    38.         {
    39.             ang = (((max - min) / 2) < ang) ? max : min; //Using the ternary operator, if the angle value is further along than half way assign it the max value, otherwise give it the min.
    40.         }
    41.         return ang;
    42.     }
    43.  
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.         swivelGuns();
    49.     }
    50.  
    51.     void swivelGuns()
    52.     {
    53.         float x, y;
    54.         Utilities.MOUNT_NORMAL gunNormal;
    55.         for (int i = 0; i != bulletSpawns.Count; i++)
    56.         {
    57.             gunNormal =  gunScript[i].normal;
    58.             gunLookers[i].transform.LookAt(GunController.worldTarget);
    59.             x = gunLookers[i].transform.localEulerAngles.x; //X value here is up/down from camera's perspective!
    60.             y = gunLookers[i].transform.localEulerAngles.y; //Y is left/right
    61.  
    62.           //  print("Weapon: " + bulletSpawns[i].name  + "====" );
    63.            // print("[A]  X= " + x + " Y= " + y);
    64.        
    65.  
    66.             if (gunNormal == Utilities.MOUNT_NORMAL.UP)
    67.             {
    68.                 x = clampAngle(x, xMin, xMax);
    69.                 y = clampAngle(y, yMin, yMax);
    70.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    71.                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    72.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    73.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    74.             }
    75.             else if (gunNormal == Utilities.MOUNT_NORMAL.RIGHT)
    76.             {
    77.                 x = clampAngle(x, xMin, xMax);
    78.                 y = clampAngle(y, yMin, yMax);
    79.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    80.                                                 new Vector3( 0f, y,0f), Time.deltaTime * angleDelta);
    81.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    82.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    83.             }
    84.             else if (gunNormal == Utilities.MOUNT_NORMAL.LEFT)
    85.             {
    86.                 x = clampAngle(x, xMin, xMax);
    87.                 y = clampAngle(-y, yMin, yMax);
    88.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    89.                                                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    90.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    91.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    92.             }
    93.             else if (gunNormal == Utilities.MOUNT_NORMAL.DOWN)
    94.             {
    95.                 x = clampAngle(-x, xMin, xMax);
    96.                 y = clampAngle(-y, yMin, yMax);
    97.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    98.                                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    99.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    100.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    101.             }
    102.          //  print("[B] X= " + x + " Y= " + y);
    103.  
    104.         }
    105.     }
    106. }
    107.  
     
    Last edited: Jan 10, 2016
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    use verbose topic titles since the forums are full of bugs.
     
  3. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    Oops! How do you change the title of a thread? I can do that can't I?
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,795
    You should use the bug reporter tool in unity. Attach all this information. Post the bug id in here.
    Bugs reported in the forum can get missed.
     
  5. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    Oh ok, didn't know of that, this honestly is the first bug I've ever discovered, and I'm still quite surprised I found it!
    Will do.
     
    karl_jones likes this.
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,795
    Are you able to recreate the problem in a very simple way? So i can try it myself.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    thread tools, top right...
     
  8. Braineeee

    Braineeee

    Joined:
    Nov 9, 2014
    Posts:
    1,211
    Yes. Its in the video. On line #30 of my SwivelGuns.CS file, if you simply enable it be removing the comment double slash ("//") it doesn't have this spastic behavior. If you disable it then it becomes a problem.

    The code I'm working with:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SwivelGuns : MonoBehaviour
    6. {
    7.     List<GameObject> barrels = new List<GameObject>(); //Refers to barrel objects
    8.     List<GameObject> bases = new List<GameObject>(); //Refers to base objects
    9.     List<GameObject> bulletSpawns = new List<GameObject>(); //Refers to barrel bullet spawn points
    10.     List<GameObject> gunLookers = new List<GameObject>();
    11.     GunController shipsTarget;
    12.     public float xMin, xMax, yMin, yMax;
    13.     public float angleDelta;
    14.     Gun[] gunScript;
    15.  
    16.     // Use this for initialization
    17.     void Awake()
    18.     {
    19.         bases = GunController.getChildObjectsWithTag(gameObject, "GunBase");
    20.         barrels = GunController.getChildObjectsWithTag(gameObject, "GunBarrel");
    21.         bulletSpawns = GunController.getChildObjectsWithTag(gameObject, "BulletSpawn");
    22.         gunScript = gameObject.GetComponentsInChildren<Gun>();
    23.         gunLookers = GunController.getChildObjectsWithTag(gameObject, "GunLooker");
    24.         shipsTarget = gameObject.GetComponentInParent<GunController>() as GunController;
    25.     }
    26.  
    27.     public static float clampAngle (float ang, float min, float max)
    28.     { //Clamps the angle to the specified range! Works perfectly!
    29.         float n = Mathf.Round(Mathf.Abs(ang / 360f)); //Find how ang goes in to 360
    30.      //  print("N = " + n); //  <  <  Problem lies with this line
    31.         ang = Mathf.Abs(ang);
    32.      
    33.         if(ang >= 360f)  //If above 360
    34.         {
    35.             ang -= (360f * n); //Force the value down to less than 360
    36.             //print("Angle was multiple of 360.");
    37.         }
    38.  
    39.         if (ang < max && ang > min) //If in outside the range of the min and max
    40.         {
    41.             ang = (((max - min) / 2) < ang) ? max : min; //Using the ternary operator, if the angle value is further along than half way assign it the max value, otherwise give it the min.
    42.         }
    43.         return ang;
    44.     }
    45.  
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         swivelGuns();
    51.     }
    52.  
    53.     void swivelGuns()
    54.     {
    55.         float x, y;
    56.         Utilities.MOUNT_NORMAL gunNormal;
    57.         for (int i = 0; i != bulletSpawns.Count; i++)
    58.         {
    59.             gunNormal =  gunScript[i].normal;
    60.          
    61.             gunLookers[i].transform.LookAt(shipsTarget.worldTarget);
    62.             x = gunLookers[i].transform.localEulerAngles.x; //X value here is up/down from camera's perspective!
    63.             y = gunLookers[i].transform.localEulerAngles.y; //Y is left/right
    64.  
    65.           //print("Weapon: " + bulletSpawns[i].name  + "====" );
    66.            // print("[A]  X= " + x + " Y= " + y);
    67.          
    68.  
    69.             if (gunNormal == Utilities.MOUNT_NORMAL.UP)
    70.             {
    71.                 x = clampAngle(x, xMin, xMax);
    72.                 y = clampAngle(y, yMin, yMax);
    73.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    74.                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    75.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    76.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    77.             }
    78.             else if (gunNormal == Utilities.MOUNT_NORMAL.RIGHT)
    79.             {
    80.                 x = clampAngle(x, xMin, xMax);
    81.                 y = clampAngle(y, yMin, yMax);
    82.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    83.                                                 new Vector3( 0f, y,0f), Time.deltaTime * angleDelta);
    84.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    85.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    86.             }
    87.             else if (gunNormal == Utilities.MOUNT_NORMAL.LEFT)
    88.             {
    89.                 x = clampAngle(x, xMin, xMax);
    90.                 y = clampAngle(-y, yMin, yMax);
    91.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    92.                                                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    93.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    94.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    95.             }
    96.             else if (gunNormal == Utilities.MOUNT_NORMAL.DOWN)
    97.             {
    98.                 x = clampAngle(-x, xMin, xMax);
    99.                 y = clampAngle(-y, yMin, yMax);
    100.                 bases[i].transform.localEulerAngles = Vector3.Slerp(bases[i].transform.localEulerAngles,
    101.                                                 new Vector3(0f, y, 0f), Time.deltaTime * angleDelta);
    102.                 barrels[i].transform.localEulerAngles = Vector3.Slerp(barrels[i].transform.localEulerAngles,
    103.                     new Vector3(x, 0f, 0f), Time.deltaTime * angleDelta);
    104.             }
    105.          // print("[B] X= " + x + " Y= " + y);
    106.  
    107.         }
    108.     }
    109. }
    110.  
    I did file a bug report, I gave them my project but I had forgotten the project was broken at the time. The biggest problem was with the BulletState file, but that isn't the problem so it can just be removed to see the bug.