Search Unity

If (transform.position.x <= - int) does not apply.

Discussion in 'Editor & General Support' started by voltage, Dec 1, 2011.

  1. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    515
    Title says all, I've been coding in c# with unity for about a month now and just realized that whenever I attempt to state an If statement by a position of a negative integer with a <= sign, the function does not apply. Has anyone else had this issue? If so, how can we let unity know to fix this issue?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There isn't an issue that I know of. If you find an actual bug, you can use the bug reporter app, but make sure it's a real bug and not a problem with your code. (Which I expect is the case here, but you haven't posted any code.) A good thing to do is make a repro case where you strip out everything except the thing that specifically causes the bug.

    --Eric
     
  3. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    515
    Well I've taken this matter seriously and proof read through everything several times. There are no outside functions related to the problem that affect this script. It's fairly simple, just playing with code. When the object reaches to -15 x, the object simply remains at a stand still. No function is initiated.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bosses : MonoBehaviour {
    5.    
    6.     public static float DownMovement = 3;
    7.     public static float LeftMovement = 2;
    8.     public static float RightMovement = 2;
    9.    
    10.     public static int BossLife = 500;
    11.     public static int BossWait = 0;
    12.     public static int Begin = 0;
    13.    
    14.     public GameObject B_Laser;
    15.    
    16.     // Use this for initialization
    17.     void OnGUI ()
    18.     {
    19.         if (Player.Score >= 100)
    20.         {
    21.             GUI.Box(new Rect(Screen.width / 2 + 250, Screen.height / 2 + 250, 150, 30),
    22.                 "");
    23.            
    24.             GUI.Label(new Rect(Screen.width / 2 + 255, Screen.height / 2 + 255, 150, 30),
    25.                       "Life: " + BossLife);
    26.         }
    27.     }
    28.    
    29.     // Update is called once per frame
    30.     void Update ()
    31.     {
    32.         if (Player.Score >= 100)
    33.         {
    34.             Function();
    35.         }
    36.        
    37.         if (Begin == 1)
    38.         {
    39.             float Left = LeftMovement * Time.deltaTime;
    40.             transform.Translate(Vector3.left * Left, Space.World);
    41.         }
    42.        
    43.         if (transform.position.x <= -15f)
    44.         {  
    45.             LeftMovement = 0;
    46.             float Right = RightMovement * Time.deltaTime;
    47.             transform.Translate(Vector3.right * Right, Space.World);
    48.         }
    49.        
    50.         else if (transform.position.x >= 15f)
    51.         {
    52.             RightMovement = 0;
    53.             LeftMovement = 2;
    54.             float Left = LeftMovement * Time.deltaTime;
    55.             transform.Translate(Vector3.left * Left, Space.World);
    56.         }
    57.     }
    58.    
    59.     void BossFight()
    60.     {
    61.         Vector3 Start = new Vector3(transform.position.x + 1.5f, transform.position.y,
    62.                                     transform.position.z);
    63.         // Quaternion.identity == Instantiated Object's original rotation.
    64.         Instantiate(B_Laser, Start, Quaternion.identity);
    65.     }
    66.  
    67.     void Function ()
    68.     {
    69.         float Down = DownMovement * Time.deltaTime;
    70.         transform.Translate(Vector3.down * Down, Space.World);
    71.        
    72.         if (transform.position.y <= 7.5f)
    73.         {
    74.             DownMovement = 0;
    75.             Begin = 1;
    76.             BossFight();
    77.         }
    78.     }
    79. }
    80.  
     
    Last edited: Dec 1, 2011
  4. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    This code in Update() makes your boss move left and right in a pre-defined pattern, slowly advancing down the screen, towards the player, is that correct? If so, there appear to be several glaring logical errors in your code.

    Also, any particular reason you are using static variables?
     
  5. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    515
    actually, the boss scrolls down into view of the camera. (The game is orthographic) From that point on, the downmovment variable is set to 0 to stand still, and then the process of moving left - right is initiated. After of course, it's laser is instantiated.

    I took tutorials on 3d buzz, I was taught to make them static, I'll admit I still am new.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The code you have tells it to stop moving left/right when transform.position.x is <= -15, so it's working perfectly as far as that goes. Variables shouldn't be static unless there's a compelling reason why there should only be one instance of them. Also, it's good practice to use lowercase for variable names, to distinguish them from uppercase class and function names.

    --Eric
     
  7. justinlloyd

    justinlloyd

    Joined:
    Aug 5, 2010
    Posts:
    1,680
    Understood.

    This is my understanding of how the code flows: Update is called every frame, nothing happens until the player's score hits 100 points or more, at which point for the rest of the fight Function function is invoked, the boss moves down the screen until he reaches a particular height, and then he stops moving down, the Function function sets the down movement speed to 0 to prevent any further descent, the flag Begin is toggled on, and a laser is invoked every update frame from this point on via the BossFight function. Now the boss fight has begun, the flag Begin is on, and the boss moves to the left. When the far left position is reached, the left movement value is set to zero to prevent any further movement to the left, and boss is nudged just a little to the right (probably not what you want) just once. The code to test if the boss has reached the extreme right will never execute because he never gets to move right other than that initial nudge, but if it were executing correctly, the boss would reach the extreme right, and then switch direction by setting the left movement value and zeroing out the right movement value.

    Is that correct?

    Is the intention to have the boss move left and right multiple times or just the once?

    Issues that I observe:
    1. You instantiate a laser blast every update frame, which if it is running at 2,000 FPS means you instantiate 2,000 laser blasts every second. Actually, it wouldn't run at that speed for very long because you are instantiating a new laser blast every frame.
    2. You never set the right movement value after zeroing it out at the extreme left so if your code were correct the boss would only ever move left, right, left and then stop.
    3. The boss reaches the far left, moves right a little, then stops forevermore because there's no code to move it right that isn't encapsulated in a test to see if the position is less than the far left.

    My solution to the problem. Note that this is untested code:
    Code (csharp):
    1.  
    2. public class Bosses : MonoBehaviour
    3. {
    4.     // how low should the boss descend before it starts strafing
    5.     public float m_descentHeight;
    6.  
    7.     // how far to the left and right should the boss strafe
    8.     public float m_strafeExtent;
    9.    
    10.     // how fast vertically and horizontally should the boss move
    11.     public Vector2 m_movementSpeed;
    12.    
    13.     // reference to the laser prefab the boss will use to fire with
    14.     public GameObject m_laser;
    15.    
    16.     // how much life energy does the boss have
    17.     public int m_bossLife;
    18.    
    19.     // how often does the boss fire
    20.     public float m_firingDelay;
    21.  
    22.     // current direction of movement
    23.     private float m_strafeDirectionDelta;
    24.    
    25.     // how long until we can fire again
    26.     private float m_firingCountdown;
    27.    
    28.     // what state are we in
    29.     private State m_state;
    30.  
    31.     void Reset()
    32.     {
    33.         m_movementSpeed = new Vector2(2, 3);
    34.         m_bossLife = 500;
    35.         m_strafeExtent = 15.0f;
    36.         m_descentHeight = 7.5f;
    37.         m_firingDelay = 2.0f;
    38.     }
    39.  
    40.     protected enum State
    41.     {
    42.         Idling,
    43.         Descending,
    44.         Strafing,
    45.     }
    46.  
    47.  
    48.     void Start()
    49.     {
    50.         m_state = State.Idling;
    51.     }
    52.  
    53.     private void Fire()
    54.     {
    55.         Vector3 startPos = transform.position + new Vector3(1.5f, 0.0f, 0.0f);
    56.         GameObject.Instantiate(m_laser, startPos, Quaternion.identity);
    57.         m_firingCountdown = m_firingDelay;
    58.     }
    59.  
    60.     private bool CanFire()
    61.     {
    62.         return (m_firingCountdown <= 0.0f);
    63.     }
    64.  
    65.     private void AttemptToFire()
    66.     {
    67.         if (CanFire())
    68.         {
    69.             AttemptToFire();
    70.         }
    71.  
    72.     }
    73.  
    74.     private void Strafing_Handler()
    75.     {
    76.         if (transform.position.x <= -m_strafeExtent)
    77.         {
    78.             m_strafeDirectionDelta = m_movementSpeed.x;
    79.         }
    80.         else if (transform.position.x >= m_strafeExtent)
    81.         {
    82.             m_strafeDirectionDelta = -m_movementSpeed.x;
    83.         }
    84.  
    85.         // Alternative version that may have a bug in it!
    86.         //if ((transform.position.x <= -m_strafeExtent) || (transform.position.x >= m_strafeExtent))
    87.         //{
    88.         //    m_strafeDirectionDelta = -m_strafeDirectionDelta;
    89.         //}
    90.  
    91.         transform.Translate(Vector3.left * m_strafeDirectionDelta * Time.deltaTime, Space.World);
    92.         AttemptToFire();
    93.     }
    94.  
    95.     private void Descending_Handler()
    96.     {
    97.         transform.Translate(Vector3.down * m_movementSpeed.y * Time.deltaTime, Space.World);
    98.         AttemptToFire();
    99.  
    100.         if (transform.position.y <= m_descentHeight)
    101.         {
    102.             m_state = State.Strafing;
    103.         }
    104.  
    105.     }
    106.  
    107.     private void Idling_Handler()
    108.     {
    109.         if (Player.Score >= 100)
    110.         {
    111.             m_state = State.Descending;
    112.             m_strafeDirectionDelta = -m_movementSpeed.x;
    113.             // give it a little delay before the boss starts firing
    114.             m_firingCountdown = 3.0f;
    115.         }
    116.  
    117.     }
    118.  
    119.     void Update()
    120.     {
    121.         m_firingCountdown -= Time.deltaTime;
    122.         switch (m_state)
    123.         {
    124.             case State.Idling:
    125.                 Idling_Handler();
    126.                 break;
    127.  
    128.             case State.Descending:
    129.                 Descending_Handler();
    130.                 break;
    131.  
    132.             case State.Strafing:
    133.                 Strafing_Handler();
    134.                 break;
    135.         }
    136.  
    137.     }
    138.  
    139.     void OnGUI()
    140.     {
    141.         if (Player.Score >= 100)
    142.         {
    143.             GUI.Box(new Rect(Screen.width / 2 + 250, Screen.height / 2 + 250, 150, 30), "");
    144.  
    145.             GUI.Label(new Rect(Screen.width / 2 + 255, Screen.height / 2 + 255, 150, 30), System.String.Format("Life: {0}", m_bossLife));
    146.         }
    147.     }
    148.  
    149. }
    150.  
     
    Last edited: Dec 1, 2011
  8. voltage

    voltage

    Joined:
    Nov 11, 2011
    Posts:
    515
    I'll definately be studying your script Justinlloyd. Thanks to you both for your input.