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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Move 2d cube the opposite way of path when boolean is triggered

Discussion in 'Scripting' started by subzer0, Jan 12, 2019.

  1. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    Hello,

    I have a cube in 2d and I attached to it a script so that it can follow a path.

    Everything is fine and the cube is moving properly in the path.

    I have a boolean which checks the direction of the cube (normal direction or reverse)

    The problem is that if I trigger the boolean the cube is not changing the direction immediately.

    Instead it starts moving when it reaches the next waypoint.

    Here is the code:

    Code (CSharp):
    1. [SerializeField]
    2.     Transform[] waypoints;
    3.  
    4.     [SerializeField]
    5.     float moveSpeed = 2f;
    6.  
    7.     [SerializeField]
    8.     int waypointIndex = 0;
    9.  
    10.     [SerializeField]
    11.     bool normal;
    12.  
    13.     private void Update()
    14.     {
    15.         Move();
    16.     }
    17.  
    18.     void Move()
    19.     {
    20.         if(normal)
    21.         {
    22.             transform.position = Vector2.MoveTowards(transform.position,waypoints[waypointIndex].transform.position,moveSpeed * Time.deltaTime);
    23.  
    24.             if(transform.position == waypoints[waypointIndex].transform.position)
    25.             {
    26.                 if(normal)
    27.                 {
    28.                     waypointIndex += 1;
    29.                 }
    30.                  
    31.             }
    32.  
    33.             if(waypointIndex == waypoints.Length)
    34.             {
    35.                 waypointIndex = 0;
    36.             }
    37.         }
    38.  
    39.         else
    40.         {
    41.             transform.position = Vector2.MoveTowards(transform.position,waypoints[waypointIndex].transform.position,moveSpeed * Time.deltaTime);
    42.          
    43.             if(transform.position == waypoints[waypointIndex].transform.position)
    44.             {
    45.                waypointIndex -= 1;
    46.             }
    47.  
    48.             if(waypointIndex == -1)
    49.             {
    50.                 waypointIndex = waypoints.Length-1;
    51.             }
    52.          
    53.         }
    54.     }
    Here is a screenshot:

     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @subzer0

    "Instead it starts moving when it reaches the next waypoint."

    Is this code written by you or did you follow some tutorial or something like that?

    I think if you read the code yourself line by line carefully, you'll see what/where the problem is.

    Where/when does a condition in your code allow to change the waypointIndex?
     
  3. subzer0

    subzer0

    Joined:
    Dec 10, 2010
    Posts:
    94
    @eses
    I know. It is changing when the position of the cube is equal to position of the waypoint.

    I want it to move immediately after I change the boolean.

    The code is from a tutorial on youtube but I edited the code a bit.