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

Question A problem in moving objects across a defined path

Discussion in 'Scripting' started by aihamnaji, May 9, 2020.

  1. aihamnaji

    aihamnaji

    Joined:
    Mar 6, 2020
    Posts:
    3
    Hello everyone.
    I am trying to create movement along path from one point to another using the following script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class FollowThePath : MonoBehaviour {
    4.  
    5.     // Array of waypoints to walk from one to the next one
    6.     [SerializeField]
    7.     private Transform[] waypoints;
    8.  
    9.     // Walk speed that can be set in Inspector
    10.     [SerializeField]
    11.     private float moveSpeed = 2f;
    12.  
    13.     // Index of current waypoint from which Enemy walks
    14.     // to the next one
    15.     private int waypointIndex = 0;
    16.  
    17.     // Use this for initialization
    18.     private void Start () {
    19.  
    20.         // Set position of Enemy as position of the first waypoint
    21.         transform.position = waypoints[waypointIndex].transform.position;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     private void Update () {
    26.  
    27.         // Move Enemy
    28.         Move();
    29.     }
    30.  
    31.     // Method that actually make Enemy walk
    32.     private void Move()
    33.     {
    34.         // If Enemy didn't reach last waypoint it can move
    35.         // If enemy reached last waypoint then it stops
    36.         if (waypointIndex <= waypoints.Length - 1)
    37.         {
    38.  
    39.             // Move Enemy from current waypoint to the next one
    40.             // using MoveTowards method
    41.             transform.position = Vector2.MoveTowards(transform.position,
    42.                waypoints[waypointIndex].transform.position,
    43.                moveSpeed * Time.deltaTime);
    44.  
    45.             // If Enemy reaches position of waypoint he walked towards
    46.             // then waypointIndex is increased by 1
    47.             // and Enemy starts to walk to the next waypoint
    48.             if (transform.position == waypoints[waypointIndex].transform.position)
    49.             {
    50.                 waypointIndex += 1;
    51.             }
    52.         }
    53.     }
    54. }
    However, this part is not being triggered no matter what i tried:

    Code (CSharp):
    1.             if (transform.position == waypoints[waypointIndex].transform.position)
    2.             {
    3.                 waypointIndex += 1;
    4.             }
    It only gets triggered when i put it in the update() before the movs function. If i add it after the move function in the update() it never gets triggered and the object never moves just like its original form.

    What am i doing wrong here?
     
  2. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,337
    You're testing floating points for equality. That's what you're doing wrong. They'll never be exactly equal to each other.

    Compute distance to waypoing, and if it is below certain threshold, move to the next one.
     
  3. aihamnaji

    aihamnaji

    Joined:
    Mar 6, 2020
    Posts:
    3

    Thank you for the feedback... i got it to work by calculating the distance and comparing it to be smaller than a certain value.