# Scripting NPC's back and forth

Hi everyone,

I am currently learning the scripting in Javascript on Unity3D, so I’m trying to make an easy 2D platformer.
I have an NPC I would like to make moving back and forth on a portion of my platform.
The game is actually taking place on the Z axis.

I easily made my character moving from his first place to a second point, named Point B (position: 0, -9, -18), and stop. His first place is supposed to be Point A (position: 0, -9, -8).
Here is what I have done so far:

``````var speedNPCtoB : float = -0.1;
private var pointA = -8;
private var pointB = -18;

function Update ()
{
if (this.transform.position.z > pointB)
this.transform.position.z += speedNPCtoB;
}
``````

I just want him to move from Point A to Point Band going back.

Anyone have any idea about how to do this?

Vector3.Lerp

Thank you for your quick reply. Now my NPC is moving from point A to point B. That’s great, a good start, thank you. Here is what I have now:

``````var pointA : Transform;
var pointB : Transform;
var speedNPC : float = 0.3;

function Update ()
{
transform.position = Vector3.Lerp (pointA.position, pointB.position, Time.time * speedNPC);
}
``````

And now the tricky part: making him going back. I tried several “if” conditions, but without success, like this:

``````var pointA : Transform;
var pointB : Transform;
var speedNPC : float = 0.3;

function Update ()
{
transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
if (transform.position.z == pointA.position)
this.transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
if (transform.position.z == pointB.position)
this.transform.position = Vector3.Lerp(pointB.position, pointA.position, Time.time * speedNPC);
}
``````

Quite hard for a non-programmer…

I’m no expert so my solution might not be the fanciest.
You should probably not use function Update() instead use another trigger, Start() perhaps. Using Update triggers the script all the time, you only want to trigger it once and then loop it until you want the NPC to do something else.
You can check how long it takes for him to move from a to b and add:

``````var walkTime : float = 5.0
transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);
yield WaitForSeconds(walkTime);
transform.position = Vector3.Lerp(pointB.position, pointA.position, Time.time * speedNPC);
yield WaitForSeconds(walkTime);
``````

either put in a loop or trigger the script again after the second yield.

Thank you for your help. I tried your script but it only placed my NPC at the point A. Doesn’t make it move.

Can this help?

``````var pointA : Transform;
var pointB : Transform;
var speedNPC : float = 5;
var minDistance : float = 5;

function Update ()
{
// Moving forward
this.transform.position = Vector3.Lerp(pointA.position, pointB.position, Time.time * speedNPC);

// check if position reach
var dist = Vecto3.Distance(this.tranform.position, pointB.position);

if(dist<minDistance) {
// Change position's
var temp : Transform;
temp = pointA;
pointA = pointB;
pointB = temp;
}
}
``````

Havent testet the code…!

Thank you but doesn’t work either… my NPC still move correctly to point B, but stay at this point…

In your code, when I launch it, I have this warning: “NullReferenceException: Object reference not set to an instance of an object” concerning this line of your code:

``````var dist = Vector3.Distance(this.tranform.position, pointB.position);
``````

I’m currenlty trying something using a switch. we’ll see.

Thank you by the way for your help everybody.

yes seem i forgot transform

``````var dist = Vector3.Distance(this.tranform.position, pointB.transform.position);
``````

Use an array of transforms, then manually loop though them once min distance is reached.
C# example

``````Transform[] points;
int currentPoint = 0; // it's like a manual for loop

void Update()
{
var dist = Vector3.Distance(transform.position, points[currentPoint].position)
if(currentPoint < points.Length) // this is to check if it's less than the length of the points
{
if( dist < minDistance)
Vector3.Lerp(blah blah);
else
currentPoint++; // this will manual increment it

}
else
{
currentPoint = 0; //this is to loop it back to zero again
}
}

}
``````

You can then assign the points so that they’re in the order you wish them to be in, i.e pointsA = 0, pointsB = 1, etc

Hi guys

Using snortop’s code, I managed to get what I wanted, and even more: my NPC is now making a half-turn each time he reaches point A or B.

Here is what I got:

``````var pointA : Transform;
var pointB : Transform;
var destination : Transform;
var speedNPC : float = 1;

function Update ()
{
// Moving forward
this.transform.position = Vector3.Lerp(this.transform.position, destination.position, Time.deltaTime * speedNPC);

// Check if position is reached
var dist = Vector3.Distance(this.transform.position, destination.position);

if (dist <= 1)
{
// Changes position
if (destination == pointA)
{
destination = pointB;
transform.eulerAngles = Vector3(0,0,0);
}
else
{
destination = pointA;
transform.eulerAngles = Vector3(0,180,0);
}
}
}
``````

So thank you everyone, see you on the forum.