- Home /
How to make a tank move/follow a path?
So I have created a pathfinding program which finds a route from point A to point B which I'm sure most of you are familiar with. What I am trying to do is let my 3D tank model move along this path. My question is 1. how to make a tank move? (I've got the basics to this but tips would help) and 2. Is there some sort of scripting trick or something that allows the model to follow the path?
Thank you!
Answer by Narv · Jul 16, 2013 at 07:29 PM
Depending on how you wrote the pathfinding vs using an asset from the store such as A*. The free version of A* has an example for moving between nodes. You will most likely want to use a character controller and the SimpleMove (or move if you want) (http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.SimpleMove.html) method for the controller to move it between nodes. Or you can get the point that it's at, and the point that the tank is supposed to be moving to, and do a Vector3.Lerp(frompoint, topoint, speed) approach to move the object.
You may want to add a rigidbody and bounding volumes to handle the collision so you don't move through objects too.
okay so say I want to get from start point (2,0,-4) to (-1,0,4) end point at a speed of 5.5? how do I script for that??
Vector3 start = new Vector3(2,0,-4);
Vector3 end = new Vector3(-1,0,4);
transform.position = Vector3.Lerp(start, end, Time.deltaTime * 5.5f);
$$anonymous$$eep in $$anonymous$$d however that you still need bounding volumes to avoid collision between the character and any objects / other characters. if you have pathfinding like A* and wanted to use the Simple$$anonymous$$ove function ins$$anonymous$$d you can't do from point a to point b, you would just do a charcontroller.Simple$$anonymous$$ove(charcontroller.transform.forward * 5.5f); and you would just send it forward between nodes in a graph.
okay, this is really helping me but I need a few more things...I'm getting an error that the name charcontroller does not exist?? If this helps, I'm using the pathfinder that AngryAnt created. All I'm looking for is a script trick that makes the car move from the starting "$$anonymous$$ySeeker" to the end point "$$anonymous$$yTarget". When I press play, it finds a path that is shown with a white line..I hope this information helps you help me :) !
Sorry if I didn't make that portion clear, "charcontroller" was just a temp variable I made up as a reference to the CharacterController. I unfortunately am not sure how the AngryAnt pathfinder works. I assume that the point $$anonymous$$ySeeker and $$anonymous$$yTarget are the start and end with a series of waypoints in between. Are you aware of any way (via the documentation) to get a list of all the waypoints the pathfinder calculates? Here is an example i have from using the A* Path asset (free version) from the store:
public class $$anonymous$$oveController : $$anonymous$$onoBehavior {
private Seeker seeker;
private float speed = 100;
private CharacterController controller;
private Path path;
private float nextWaypointDistance = 3;
private int currentWaypoint;
void Start() {
seeker = GetComponent<Seeker>(); // this is from the package, a script they use for attaching an object to a path
controller = GetComponent<CharaceterController>();
}
void Update() {
if(Input.Get$$anonymous$$ouseButtonDown(0)) //what triggers a new path? i put a mouse down.. but can be whatever.
{
Vector3 newpoint = new Vector3(x,y,z); // this im leaving blank, this would be the value of your $$anonymous$$yTarget variable, however it is you are calculating that, put that here.
seeker.StartPath($$anonymous$$yStart, $$anonymous$$yTarget, OnPathComplete); // this is the function you call when calculating the path, this will change based on your AngryAnt code. start a path from $$anonymous$$yStart -> $$anonymous$$yTarget and it sets a callback for a function to call when the path is complete, i call this "OnPathComplete" but can be called whatever
} // end if
} // end mouse click code
// if there is no path to follow, end the update, not advised if you have other non-path related code in this update function
if(path == null)
return;
// if you are at the last point in the waypoint graph, end the update since you have no more paths to follow
if(currentWaypoint >= path.vectorPath.Count)
return;
// Direction to the next waypoint
Vector3 direction = (path.vectorPath[currentWaypoint] - transform.position).normalized; // this gets the direction from point A to point B and gets a vector to rotate towards
direction *= speed * Time.deltaTime;
controller.Simple$$anonymous$$ove(direction);
// rotate the tank in the direction you want it to move so it doesn't appear to glide
Vector3 rotation = new Vector3(x,y,z); // xyz values from the vectorPath
transform.rotation = Quaternion.Slerp(...); // rotate from position to new position over a time.
// check if near the next waypoint, if we are, go to the next waypoint
if(Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]) < nextWaypointDistance){
currentWaypoint++;
return;
}
} // end update function
void OnPathComplete (Path p) {
path = p;
currentWaypoint = 0;
}
} // end class
That is kind of long but it should be able to help you. I tried to comment the important parts and i got lazy towards the end rewriting the code so hopefully it makes sense the logic flow behind it.
Forgot to add, you need to put
using Pathfinding;
At the top too
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to rotate at the same rpm wheels of a tank? 1 Answer
How to turn a tank? 1 Answer