# Ray-based pathfinding

Does anyone perhaps have a script of ray based pathfinding? A simple one, so that a object will follow the player and walk around objects.. Thanks

Okay so here's the script I wrote, hope it'll be to use to someone Here it is: It should be self explaining, especially with all the added comments:P EDIT: It is written in C# so the extension should be .cs and the name should be EnemyFollowerNew so you'll get EnemyFollowerNew.cs You have to drag the target object into the target slot too or else it just won't follow you:)

If anything still isn't clear let me know.

``````using UnityEngine;
using System.Collections;

public class EnemyFollowerNew : MonoBehaviour
{
// Variables
public float speed = 20;
public float rotateSpeed = 10;
private int dIrection = -1;
public float maxDistance = 5;
public float directionDistance = 5;
public float targetDistance = 5;
public float followSpeed = 11;
public Transform Target;
// End variables

// Update is called once per frame
void Update ()
{
//Check Distance between current object and Target
float dist = Vector3.Distance(Target.position, transform.position);
//print("Distance to other:" +dist);

// If distance is bigger then distance between target, wander around.
if (dist > targetDistance)
{
//Check if there is a collider in a certain distance of the object if not then do the following
if(!Physics.Raycast(transform.position, transform.forward, maxDistance))
{
// Move forward
transform.Translate(Vector3.forward * speed * Time.smoothDeltaTime);
}
else
{
// If there is a object at the right side of the object then give a random direction
if(Physics.Raycast(transform.position, transform.right, directionDistance))
{
dIrection = Random.Range(-1, 2);
}
// If there is a object at the left side of the object then give a random direction
if(Physics.Raycast(transform.position, -transform.right, directionDistance))
{
dIrection = Random.Range(-1, 2);
}
// rotate 90 degrees in the random direction
transform.Rotate(Vector3.up, 90 * rotateSpeed * Time.smoothDeltaTime * dIrection);
}
}
// If current distance is smaller than the given ditance, then rotate towards player, and translate the rotation into forward motion times the given speed
if (dist < targetDistance)
{
transform.LookAt(Target);
transform.Translate(Vector3.forward * followSpeed * Time.smoothDeltaTime);
}

}
}

``````

When I get a chance today, I'll write one (I need something like it) and post it for you...

-Edit-

I just thought of a simple way to do it... Add this script to your "Enemy":

``````    var thePlayer : Transform;

function Update () {
transform.LookAt(thePlayer);
}

``````

And give it a constant relative force of 10, 0, 0

I will still write a script today using raycasting... I haven't tested the method I put above, but it might work... : )

hey, I'm looking for such a script too! I would like to get something like this: