# Pathfinding, one object to another.

Working on a zombie game, and I have scripted it so that the zombie will spawn and then move towards the player. The problem is, there is no path finding and they cannot find their way around objects etc.

I have tried “Path” but I don’t know how to make it so that one object moves towards another using the path finding. A* path finding project works, But I can’t use it commercially.

Where is a tutorial or a resource that can help me create a very basic path finding script in unity?

Use RayCasts. If it hits a trigger or gameobject named Wall or whatever, move the opposite of the hit direction.

Do you have any pseudo code? That might help me out alot.

I am just a bit confussed on how to move in the oppisite of the hit direction.

thanks,
Dusk

Write your own A* algorithm is quite easy
http://theory.stanford.edu/~amitp/GameProgramming/

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

``````function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
}
}
``````

Okay, So I have it now so it sets a bool to true while there is an object in front, but now how to I handle going around it?

transform.Translate

transform.rotate

Example:

``````function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) {
transform.Rotate(2,0,0);
}
}
``````

Thanks. I will get back to this thread once I figure out how to implement this stuff

I have this:

``````var waypoint : Transform;

var speed : float = 1;

var blocked : boolean = false;

function Update ()

{

var delta = waypoint.transform.position - transform.position;

delta.Normalize();

var moveSpeed = speed * Time.deltaTime;

transform.position = transform.position + (delta * moveSpeed);

var fwd = transform.TransformDirection (Vector3.forward);

if (Physics.Raycast (transform.position, fwd, 3))

{

blocked = true;

transform.Rotate(0,1,0);

}

else

{

blocked = false;

}

}
``````

The rotation happens when the objects gets too close to a wall, but he just keeps moving towards the waypoint.

Here is my code so far, but It doesn’t really work. I have a waypoint that I have set temporarily that the object should move towards, but I’m just not sure how to handle the movement. it detects when there is a wall and when it gets too close it turns, but I’m not sure how to handle it so that it moves towards the player.

``````var waypoint : Transform;

var speed : float = 1;

var blockedFront : boolean = false;

var blockedBack : boolean = false;

var blockedLeft : boolean = false;

var blockedRight : boolean = false;

function Update ()

{

var fwd = transform.TransformDirection (Vector3.forward);

var bck = transform.TransformDirection (Vector3.back);

var left = transform.TransformDirection (Vector3.left);

var right = transform.TransformDirection (Vector3.right);

if (Physics.Raycast (transform.position, fwd, 2))

{

blockedFront = true;

}

else

{

blockedFront = false;

}

if (Physics.Raycast (transform.position, bck, 2))

{

blockedBack = true;

}

else

{

blockedBack = false;

}

if (Physics.Raycast (transform.position, left, 2))

{

blocked = true;

}

else

{

blockedLeft = false;

}

if (Physics.Raycast (transform.position, right, 2))

{

blockedRight = true;

}

else

{

blockedRight = false;

}

Movement();

}

function Movement()

{

var delta = waypoint.transform.position - transform.position;

delta.Normalize();

var moveSpeed = speed * Time.deltaTime;

//transform.LookAt(waypoint);

move normally if there is nothing blocking the character.

if(blockedFront == false  blockedRight == false  blockedBack == false  blockedLeft == false)

{

transform.Translate(Vector3.forward * moveSpeed);

}

if(blockedFront == true  blockedRight == false)

{

transform.Rotate(0,90,0);

}

}
``````

Raycasting really isn’t going to work in most cases. It may help the object navigate around small objects, but the object will still run into walls without a proper pathing algorithm. There are a number of A* tutorials out there, although if you’re just beginning programming it can be difficult to wrap your head around. If you wait until the next version of Unity, there will supposedly be built-in pathfinding. Other options include hiring a programmer specifically to implement A*, or to keep trying to find a solution which can be used commercially.