- Home /
Moving player along path?
I'm sorta' a noob, however pretty familiar with Unity3D. I'd would like to know the best way to move my player along a path?
I have an level where my player can go forward, backwards, left or right. I need my player to drive down the center of each path (my player moves automatically, the user only controls the direction; forward, backwards, left or right; think PacMan).
What are the best techniques for doing this? My paths may also be curved. Keeping the player center is important and keeping him looking forward in the direction of movement is also. I was thinking i could trace out some sort of spline along my paths, but unity doesn't have that (that I can tell). Can/should I use Unity's built-in Navigation functionality? Looks like that might only be good for patrollers or drones, not a player object. Seems pretty simple, but I'm feel'n a bit stumped on how to approach it.
Any recommendations appreciated.
Answer by FlowStateGames · Dec 10, 2012 at 07:11 PM
The simplest answer is to create a set of pathnodes. Put these pathnodes in a collection. Add a public variable of this type of collection on your player. This way you can assign them from the editor (so that you could have different paths for different scenes/behaviors). Then use the collection to iterate through them whenever you decide to move on. Here's some pseudocode:
//class declaration
public NodeCollection currentPath;
void Update(){
if(followingPath){
// evaluate your condition for moving onto the next node
UpdatePath();
// rotate player towards next node
LookAtPath();
}
}
void UpdatePath(){
// check if you're within the desired distance to the current node
if(closeEnough){
// if we're not at the end of the path
if(!currentPath.HasNext()){
currentPath.NextNode();
}
else{
// here we can deal with coming to the end of the path.
// is this the beginning of the next scene? do we loop around and start the path over?
// either way, one way to deal with it is to black box the behavior into a method
//(maybe this can even be a method from another script, so you can
//overload it with different behaviours)
EndOfLine();
}
}
}
Obviously, this is basic code. You could implement this as an FSM, where your states are something like "NoPath, ToNode, AtNode", and then your edges between states handle what to do when you get to a node.
You could also extend this for multiple paths, to have conditions which may allow you to move from one path to another, etc.
This is very basic, but has the upside of being extremely lightweight. For something a bit more complex, you could also do something similar to A* pathing (or a more realistic, slightly more complex algorithm, theta-star pathing), but this should get you started.
Let me know if any of this is obfuscated or confusing, kinda jotted this down in a hurry.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Circular movement of the player 0 Answers
How can i smooth out the rotation? 1 Answer
Player movement isn't working 1 Answer