- Home /
How can I switch paths by pressing a button?
Hi all, I am working on a personal project and having a bit of trouble on achieving something. I have a car which i want to follow a path but on a junction, i need a button that can switch the car's Path.
Can anyone please help me in the right direction or if there is any tutorial or anything. I tried finding for something similar but mostly its just path follow tutorials out there!
Are you navigating it using waypoints, unity's navigation or some other way?
Yes I am using empty game objects as nodes for my path, which the car is following.
In that case you basically need to set the point to go towards with an if statement. A simple example. If the waypoints are like this:
1 2 3 4 5
6 7 8
wayPoint++
will work for straight on.
To follow the next line you'd need something like this.
if(Input.Get$$anonymous$$eyDown("key")){
wayPoint = 6;
Answer by Anis1808 · Dec 05, 2019 at 12:37 AM
@Magso has a point, if you know the current node of the car and you store it in a variable, and you know that that node has multiple connections, then by using an if statement if(Input.GetKeyDown(KeyCode.C))
for example, you can then tell the car to use a different waypoint than the one intended (or a random point connected to the current point) which should change the direction of the car.
This is all assuming two main things:
1.That you have stored the current waypoint ID
2.That each node has a script attached to it that helps you get the nodes connected to that node so you would be able to get the next nodes using
Node[] nodes = currentnode.GetComponent<Node>().nextNodes;
foreach(Node node in nodes){
//this is a node that could be your next path.
}
You could use 1 script on the player having all the waypoints in an array or list and use Vector3.Distance
to the current waypoint to check when it close enough to go to the next one.
Yes but that is not precise enough, it could return a value behind the car and causing problems.
Your answer
Follow this Question
Related Questions
Pathfinding with Roads 2 Answers
How to make one game object follow another? 1 Answer
Super basic enemy pathfinding AI 1 Answer
Follow curved path plus also swipes 1 Answer
How To destroy specific objects? 2 Answers