Search Unity

Array question

Discussion in 'Getting Started' started by DocJ, Oct 19, 2019.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Hi, I'm working on a project where I think I need 2-3 arrays. All of the arrays will consist of waypoints. The Player will be mainly following the waypoints in the 1st array, but circumstances may cause the Player to be "diverted" to a waypoint in the 2nd or 3rd array.
    Is there a way to follow waypoints from multiple arrays? And if so, can the following move of the player be referenced (started) at the point he left the 1st array?

    I'm using the following to move my 2d Player:

    transform.position = Vector2.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);


    Example: Car driving down the street, makes a wrong turn after the 5th block. He turns around to get back to the main street before continuing on his path.

    Thanks in advance for any help
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Each array is a path. I think what you want to do is, when you are "diverted" from one path to another, find the closest point on the new path to your current location. Move to that point (good job BTW using MoveTowards; it's exactly the right way to do it), and then continue on the new path from there.

    Finding the closest point on the new path is just a matter of checking the distance to the closest point on each line segment (pair of points in your array). I have code to do that lying around somewhere if you need it.