Making a single waypoint

Could someone explain how to move a object from one position to a single waypoint? It seems like something that wouuld be really easy but I have not done it yet.

There are descriptions out there that explain how to move to multiple waypoints but not just one.

If someone could provide a script and explain how each line works that would be great.

http://lmgtfy.com/?q=Unity3D+Waypoint+System

First Result is actually a good tutorial:

I’ve actually went through steamisM50’s tutorials. Although, its hard to follow and he doesnt explain much.

This is the code for his OneWaypoint system tutorial. I’ve emailed him questions about it but he has not responded. I would really like someone to explain how his code works. The comments in the code are some questions I have.

var waypoint : Transform;
 var speed : float = 20;

 function Update () {

 var target = waypoint.position;
 var moveDirection : Vector3 = target - transform.position; //why do we
// minus the targets transform.position from moveDirection? 
// I would think this would make the enemy 
// move away from the waypoint.

 var velocity = rigidbody.velocity;

 if(moveDirection.magnitude < 1){

 velocity = Vector3.zero; // this must stop the enemy once it reaches the waypoint correct?

 }
 else{
 velocity = moveDirection.normalized * speed; // I think this is the line
// that makes the enemy go toward the waypoint, but how?
 }
 rigidbody.velocity = velocity; //Why is this neccesary? Or how come it did not work without it?
 }

Well I used his script from the start of that vid as a basis for a much larger and harder waypoint system from a line render that you draw with your mouse. It works perfectly.

Here is the code for my waypoint part,

//Above at the top
private Vector3[] waypoint;
private int currentWaypoint;


//the functions for moving along the waypoints from line renderer
	private void MoveStuff () {
		if(!passed) {
			GatherInfo();
			counterParts -=1;
			passed = true;
		}
	
		if(currentWaypoint < waypoint.Length) {
			target20 = waypoint[currentWaypoint];

			Vector3 moveDirection = target20 - transform.position;
			
			
			transform.Translate(moveDirection.normalized * movSpeed * Time.deltaTime);
			
			if(moveDirection.magnitude < .5) {
				currentWaypoint++;
				counterParts--;
			}
		}
	}
	
	private void GatherInfo () {
		counterParts = gameObject.GetComponent<PathMaker>().linePoints.Count;
		waypoint = gameObject.GetComponent<PathMaker>().linePoints.ToArray();
	}

I hope this can give a bit of an idea of how to do it, in the Gather Info it takes the linePoints (the list from the line renderer) and put them into an array that the waypoints follow.

You can ignore the counterparts variable, that is used by me for something else.

So I hope you can look through this and get an idea of how to do what you want to do.

Edit: I also used a transform instead of the rigidbody, as I don’t want many physics objects for my game.

The first thing you need to know is what exactly a vector is - to put it simply, a vector is a point in space (in your case, it’s 3D space - x, y, and z axes). A typical vector looks like this:

Vector3(1.0, 2.0, 3.0)

Each of those numbers gives us movement along an axis - they are ordered by x,y,z. That particular vector is 1 unit right along the x axis, 2 units up along the y axis, and 3 units out along the z axis(set a cube to be at that position and you can see it in the Unity editor).

Take a look at this (crude) picture of a (2-dimensional - x y) vector: http://cl.ly/1z0g1r000E2Y080D160x A pretty standard point. But what if we draw a line from the point to the origin of the axes(the corner)?
We get this: http://cl.ly/2i2Z3E0h0b1K25350j3E
The ‘magnitude of a vector’ is a fancy way of saying ‘the distance between this point in space and the origin of the axes(the corner)’.
You can also see that our vector has a direction - the angle between the axes and the point.
So there you have the conventional definition of a vector: A mathematical quantity that has both direction and magnitude. The direction is from the vector to the coordinate axes - the magnitude is the distance between the vector and the coordinate axes.

Now suppose we have a cube. And suppose we want to find out how far this cube is from the zero point in Unity. How can we do this?
First thing is to find the cube’s position vector - the point at which the cube is. This is accomplished with:

var cubePosition : Vector3 = transform.position; //transform is a component - you can see it in Unity when you select any gameObject

So we have our cube’s position as a vector. To find how far we are from the zero point, we need to find the magnitude of that vector. Unity makes it easy for us:

var distanceFromStart : float = cubePosition.magnitude; //aha! Unity knows about magnitudes!

So now we know how to separate the distance of a vector from the angle of a vector. But suppose we want the angle of a vector without the distance?

var angleFromStart : Vector3 = cubePosition.normalized; //what this does is give us our vector, but with a magnitude of 1

So now we have a vector that’s the same direction as our old vector, but it is only 1 unit away from the zero point. http://cl.ly/372u0d3N3j3i1m403Q3I

Now here’s the really cool thing about vectors - you can subtract them from each other. Suppose we have two points in space. Math types like to name their vectors, so I’ve taken the liberty of naming mine: http://cl.ly/3X1Q241H3K392g0P402r So we have Chuck and Fred - two unsuspecting vectors. What happens if we subtract Chuck from Fred, like so:

var chuckFred : Vector3 = Fred - Chuck;

We get a new vector that is the difference between Chuck and Fred (let’s name it Nick)! Remember that a vector has direction and magnitude - what is Nick’s direction and magnitude?

As you can see here: http://cl.ly/3I1N1J1C3m0k0a0j3G12 Nick’s direction is a vector that points from Chuck to Fred, and his magnitude is the distance between Chuck and Fred.

Now suppose that instead of Chuck and Fred, we have two other vectors - the position of a tank and the position of a target that we want the tank to move to. How can we make the tank move to the target?

var tankPos : Vector3; //the position of your tank
var targetPos : Vector3; //the position of your target

The first thing to do is figure out what direction we want to move. How do we do that again?

var difference : Vector3 = targetPos - tankPos; //subtract our position from the position we want to be at
var direction = difference.normalized; //Remember - we have to give our vector a magnitude of 1 - otherwise the tank would move faster if we were farther away(because the distance/magnitude is bigger).

So now we have a direction to travel that will get us to our target point. To do the actual movement:

transform.position = transform.position + direction * moveSpeed * Time.deltaTime

So what we’re doing there is taking our current position(point in space) and adding our direction vector multiplied by the speed we want to move, multiplied by TIme.deltaTime. Which is one last thing we should talk about - multiplication. When you multiply a vector by a number, you get a new vector that has the same direction as the old one, but has a magnitude that is that many times larger. So if you multiply a vector by 2, you’ll get a new vector with the same direction, but that’s two times as long.

So a complete script for moving your tank would look like this:

var target : Transform; //you can assign this in the inspector
var moveSpeed = 20.0; //how many meters per second we move

function Update(){
  var targetPos = target.position; //the vector of our waypoint
  var direction = (target.position - transform.position).normalized; //the direction we want to go

  transform.position = transform.position + direction * moveSpeed * Time.deltaTime;
}

And there you have it. :wink:
Standard disclaimers apply: Images are not guaranteed to be legible or useful, code is not guaranteed to compile, I may not have any idea what I’m talking about etc etc.

Hope that helps - post back if there’s anything you’d like explained more fully or that you didn’t understand. :wink:

Regards,

thanks for the great explanation! It took me awhile to read through.

A couple questions.

why did you parentheses around (target.position - transform.position) in the complete script?

Also, var targetPos = target.position, doesnt seem to do anything. Was that a mistake?

Also, is there a way to build a multiple waypoint system starting off with this simple code?

var target : Transform;
var speed : float = 3;

function Update () {

	var direction = (target.position - transform.position).normalized;
	transform.position = transform.position + direction * speed * Time.deltaTime;

}

Question 1:
Its all in the return value, and mathematics. Each equation is solved in normal mathematical order. (anything in parentheses are calculated first.)

In math, (5-3)6 is not the same as 5-36.

So, target.position - transform.position.normalized is not the same as (target.position - transform.position).normalized

This means… Get the result from the target position minus the transform position and return the normalized result.

There are a few good pieces of code that result from this type of equation.

target - distance == unmodified direction
(unmodified direction).normalized = mathematical direction (-1 to 1 directions)
(unmofified direction).magnitude = distance between objects

Question 2:
Yep, that is the basis of going to 1 waypoint. Imagine that when you reached it, you just set your target to the next waypoint.

There are 3 basic types of waypoint systems. Single Direction, Ping Pong and looping. you could use any or all of them if you just figure out how to set the target to the next/last or first waypoint as needed.

thanks BigMisterB

I think there is a way to set the target to the next waypoint with arrays. But how can I do this?

anyone know?

say you have a list of waypoints simply keep track of which one you are on and set the target to the waypoint you are currently on. Then every frame, just check the distance to the waypoint and get the next one in line.

You have 3 methods… one way, looping or PingPong

I could write all the code for you, but that wont teach you anything.

p.s. This is actually pretty simple in the end run, you just need to think outside the box. Generate some code and we will see where you get with it.

I wish I could figure it out myself. But i’m drawing blanks as to how to get a variable to represent the current target in the array.

This code wont compile, but its all I can think of.

var target : Transform[];
var speed : float = 3;
 var currentTarget = 0;
function Update () {

currentTarget = target[currentTarget];

if (curretTarget.position == transform.position){

currentTarget++;
}
 
    var direction = (target.currentTarget - transform.position).normalized;
    transform.position = transform.position + direction * speed * Time.deltaTime;
 
}

Could somone show me how to make a multiple waypoint system starting off with this code?

var target : Transform;
var speed : float = 3;
 
function Update () {
 
    var direction = (target.position - transform.position).normalized;
   transform.position = transform.position + direction * speed * Time.deltaTime;
 
}

can anyone help?

var target : Transform[];

var speed : float = 3;

var targetCount = 0; 
var currentTarget : Transform;

var changeTargets : float = 5.0;

function Update () {
		currentTarget = target[targetCount];
		
	var distanceVector = Vector3.Distance(currentTarget.position, transform.position);
	
	if (distanceVector <= changeTargets){
		targetCount++;
	}
		var direction = (target.currentTarget - transform.position).normalized;

		transform.position = transform.position + direction * speed * Time.deltaTime;
}

Haven’t done Javascript for a while so may be messy, but it really isn’t that hard. The tutorials I posted up top is a great way to do it but u can do it your way as well.

Edit: BTW you cant set a number to a transform…

when I try to your code I get a null reference exeption at line 18. I dont see the problem though.

I’ve tried the tutorials your mentioning. But I cant gain any knowledge from them because they dont explain the code. I’ve attempted to understand by just looking at it, but cant figure it out. The only thing I can do is copy and paste the code which doesnt help me learn.

bump

hello?

Did you add the GameObject to the Transform in the Inspector? That is showing on line 18?