How to move a GamePlayer object to adjacent node and not a random other node?

Hi all,
Starting to go insane and have killed too much trying so now hopefully some good people can help me out!

Just trying to create a map where the player can click on a node (cylinder\circle) adjacent to where they are and move to it. Some Nodes go direct to each other but some nodes you have a choice, highlighted in red.

But instead how I have it setup is I can click on any node and it goes straight to it in a straight line, highlighted in blue.

The purple vector lines are for the viewport so I can see which nodes you can move between, they are just a guide. Mind you it’d be great in the GamePlayer (Cube that is selected) could follow that line too.

I have a script attached to the Route parent and nodes as children, and to the GamePlayer parent for the cube.

Any guidance or tutorials for this?

I followed this tutorial to start, but really need\want the functionality of clicking and moving to any adjacent Node only.

Thanks.

If you want the player to input the sequence of moves they will do, then you need some type of stateful input gathering mechanism that accumulates your clicks into a list, checks for validity, then considers it complete either when you say or when you reach the goal.

You would perhaps want a way to back up one link, or to escape and quit all input, start over, and perhaps a way to confirm: “hey, this is the path you just put in… do you want to do it now?”

1 Like

Thanks for your response @Kurt-Dekker
The way I have laid it out is pretty much the same as the boardgame YT clip above, do you think this is the right approach?

It sounds like you want to represent this play area as a graph. Each node can contain a list of other nodes that it is directly connected to. When the user clicks on a node, you only allow the movement if the selected node is on the list of directly connected nodes for the current position.

public class GameNode : MonoBehaviour
{
    //If all links are bi-directional, then you only need one list of connected nodes
    [SerializeField] List<GameNode> outgoing;
    [SerializeField] List<GameNode> incoming;

    public bool CanIMoveHere(GameNode node)
    {
        return outgoing.Contains(node);
    }
}

You could also implement the graph structure as a separate, non-MonoBehaviour entity and use that to validate the game rules. You will still need a way to associate the nodes in such a graph to the actual game objects in your scene.

2 Likes

^ ^ ^ ^ exactly this.

But do things in stages.

First is a bunch of disconnected “spots” on the world.

Clicking on one should say “send me to spot X!”

First version of your game should teleport the player to that new spot, without checking ANY rules.

Only when you have that working 100% should you consider the next step: validation.

Validation happens through the graph.

“I am at spot Y and I want to go to spot X, is there a link?”

Get that working:

  • teleport to spot X if the link exists, OR…
  • complain and say you can’t get there

Once that works and you teleport to the new spot, now you are ready for the next part:

Use a tweener to smoothly move to the new spot.

Finally when all the above works perfectly, set a boolean when the tween starts so you ignore all further clicking until you arrive.

This is the essence of iterative game development: one success built upon another.

If you try to do three things in a row and one of them fails, it becomes very difficult to reason about which one failed and why.

1 Like