Search Unity

Waypoint question

Discussion in 'Scripting' started by tawdry, Nov 21, 2017.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi
    Say I have a large network of waypoints and I want a character to move along them but don't want to hardcode it.For instance the character is off to the gym but then he gets a call and has to head to a friends house.Now how would i be able to get him to the house from any random waypoint then after he might continue on to the gym.

    Easiest way would be to assign the destination(gym,house) as the next target but how could I make him follow the waypoints between the final destination and his position now?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are your waypoints sequential in the order you'd want them to follow .. to friend's or gym ? It sounds like that, from the way you worded it.
    You need the destination's index (somehow).. maybe a script on the destination that stores it or it's in a dictionary by name, not sure how it works for you. Then, I'd just check if it's higher or lower than where I am, and start sending the character en route in the right direction?
    If it's more of a grid than a line, then similar logic but.. you know.. with a grid :)

    Is that at all helpful? Did I understand your question?
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    This is a concept in game development called pathfinding and there are countless solutions.

    For a simple waypoint system, you need to construct a graph of connected nodes to know what goes where. From there, you can use Dijkstra's algorithm to find paths.
     
    TonyLi and KelsoMRK like this.