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

Enemy Movement Bug

Discussion in 'Scripting' started by LarryWells, Jun 19, 2016.

  1. LarryWells

    LarryWells

    Joined:
    Jun 1, 2016
    Posts:
    30
    NOTE: I know my code isn't the cleanest I just started out a month ago.

    I am trying to make this cube move between two game objects in a pong type fashion. So far it hits the right edge, goes to the left edge and when it hits the left edge it some how increases speed and when it hits the right edge the game object is flung off the map. I have done this like 400 times but I am tired and I must be missing something simple but I don't want to go to bed before this bug is fixed.

    Code (CSharp):
    1.  
    2. public class EnemyMovementLevelOne : MonoBehaviour {
    3.    
    4.     public int MovementSpeed = 20;
    5.     bool hitRight = false;
    6.     bool hitLeft = false;
    7.     void Update () {
    8.         this.transform.Translate (MovementSpeed * Time.deltaTime, 0, 0);
    9.         if (hitRight == false) {
    10.             this.transform.Translate (MovementSpeed * Time.deltaTime, 0, 0);
    11.         } else if (hitRight == true) {
    12.             this.transform.Translate (-MovementSpeed * Time.deltaTime, 0, 0);
    13.         }
    14.         if (hitLeft == true) {
    15.             this.transform.Translate (MovementSpeed * Time.deltaTime, 0, 0);
    16.         } else if (hitLeft == false) {
    17.             this.transform.Translate (-MovementSpeed * Time.deltaTime, 0, 0);
    18.         }
    19.     }
    20.     void OnTriggerEnter (Collider other) {
    21.         if (other.gameObject.name == "RightEdge") {
    22.             Debug.Log ("Hit the right edge");
    23.             hitRight = true;
    24.         } else if (other.gameObject.name == "LeftEdge") {
    25.             Debug.Log ("Hit the left edge");
    26.             hitLeft = true;
    27.         }
    28.     }
    29.     void OnTriggerExit (Collider other) {
    30.         if (other.gameObject.name == "RightEdge") {
    31.             hitLeft = false;
    32.         }
    33.         if (other.gameObject.name == "LeftEdge") {
    34.             hitRight = false;
    35.         }
    36.  
    37.     }
    38. }
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Try this
    Code (CSharp):
    1. void Update () {
    2.     if (hitRight) {
    3.         this.transform.Translate (-MovementSpeed * Time.deltaTime, 0, 0);
    4.     } else if (hitLeft) {
    5.         this.transform.Translate (MovementSpeed * Time.deltaTime, 0, 0);
    6.     } else {
    7.         this.transform.Translate (-MovementSpeed * Time.deltaTime, 0, 0);
    8.     }
    9. }
    10.  
    Or a cleaner way

    Code (CSharp):
    1. public float MovementSpeed = 20;
    2.  
    3.     private Vector3 velocity;
    4.  
    5.     void Start()
    6.     {
    7.         velocity = new Vector3(MovementSpeed, 0, 0);
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         this.transform.Translate(velocity);
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.         if (other.gameObject.name == "RightEdge")
    18.         {
    19.             velocity = -velocity;
    20.         }
    21.         else if (other.gameObject.name == "LeftEdge")
    22.         {
    23.             velocity = -velocity;
    24.         }
    25.     }
    This way you start with positive speed at the x axis, and everytime you collide with either edge, it changes direction on the x axis.

    It should work assuming OnTriggerEnter () does not get called twice before you exist the colission with the edge.
     
  3. LarryWells

    LarryWells

    Joined:
    Jun 1, 2016
    Posts:
    30
    Thanks, the first one had the same bug BUT the second way worked perfect after I added * Time.deltaTime to the new Vector3. <3