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

If statement - old variable vs new variable

Discussion in 'Scripting' started by mhedges, Jun 2, 2018.

  1. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Hello!

    I'm learning Unity, and porting some apps I did in another language to this one. One of the apps is Space Quartz, an Asteroids clone. I have the ship and asteroids working, and am currently working on the UFOs.

    I am stuck on the UFO random navigation. It should be as follows: the UFO spawns based on a timer. It then chooses to go either left or right (it spawns on the edge of the screen: it has a screen wrap or euclidean torus). After moving horizontally for a fraction of time, it chooses a random value from three. One for up, one for stay level, one for down.

    The UFO should not go above or under a certain angle (should not go steeper), so it should check the existing random value, and if the new random value is the same, then it goes along in the same direction, not steeper. I hope you understand thus far, because here's the code for the movement:

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class LargeUFOMovement : MonoBehaviour {
    5.  
    6.     public GameObject LargeUFO;
    7.     private float LULeftRight;
    8.     private float LUUpDown;
    9.     private int OldVal = 0;
    10.     private int NewVal;
    11.     private int LUFOSpeed = 60;
    12.     private float nextActionTime = 0.0f;
    13.     private float period = 0.5f;
    14.     Rigidbody2D LUrb2d;
    15.  
    16.     void Start () {
    17.  
    18.         LUrb2d = GetComponent<Rigidbody2D>();
    19.         LULeftRight = Random.value;
    20.  
    21.         if (LULeftRight < 0.5)  {
    22.             LULeftRight = -1.0f;
    23.  
    24.             GetComponent<Rigidbody2D> ()
    25.                 .AddRelativeForce (transform.right * LUFOSpeed * LULeftRight);
    26.         }
    27.         if (LULeftRight >= 0.5) {
    28.             LULeftRight = 1.0f;
    29.             GetComponent<Rigidbody2D> ()
    30.                 .AddRelativeForce (transform.right * LUFOSpeed * LULeftRight);
    31.         }
    32.     }
    33.    
    34.  
    35.     void Update (){
    36.         if (Time.time > nextActionTime) {
    37.             nextActionTime += period;
    38.             MoveUp ();
    39.         }
    40.     }
    41.        
    42.     public void MoveUp(){
    43.         NewVal = Random.Range (1, 4);
    44.  
    45.         if (NewVal == 1) {
    46.             if (NewVal != OldVal) {
    47.                 LUUpDown = -1.0f;
    48.             }
    49.         }
    50.         if (NewVal == 2){
    51.             if (NewVal != OldVal) {
    52.                 LUUpDown = 0.0f;
    53.             }              
    54.         }
    55.         if (NewVal == 3){
    56.             if (NewVal != OldVal) {
    57.                 LUUpDown = -1.0f;
    58.             }
    59.         }
    60.  
    61.         OldVal = NewVal;
    62.         GetComponent<Rigidbody2D> ()
    63.             .AddRelativeForce (transform.up * LUFOSpeed * LUUpDown);
    64.  
    65.     }
    66. }    
    Do not mind the private float period of 0.5f; if should be longer, but I put in 0.5f as to not have to wait for so long to observe the behavior.

    Since this behavior will be used in the Small UFO (but changing the UFO's speed), you will have helped me twice! Thanks, regards.
     
  2. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Edit: line 57 - LUUpDown = 1.0f; not -1.0f .
     
  3. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Before line 62 (where the new force is applied), you could try resetting the vertical velocity component to zero:
    Code (CSharp):
    1. Vector3    v = rigidbody.velocity;
    2. v.y = 0f;
    3. rigidbody.velocity = v;
     
  4. mhedges

    mhedges

    Joined:
    Feb 23, 2017
    Posts:
    13
    Worked! Thanks much, regards.
     
    Doug_B likes this.