# Enemy to move out of way of player

Hey guys, looking for some help with an idea.

Basicly, i’m hoping to have asome insects on the ground, and when the player moves near then they scutter away, and then when player has walked past they return to the area they were in. All the time, they’re movinga bout within a set space.

However, I’m a little stuck. The bugs should be raycasting for object with tag Player - in all directions, however I do not know how this is done. Below is attempt2/34!

Script:

``````  public var waiting : boolean = false;
var randomPosition : Vector3;
var moveDelay : float = 1;
function Start(){
}

function Update(){
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.forward, hit, 1)) {
var distanceToBall = hit.distance;
if( distanceToBall <= 1){
transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*10);
}
}
}

function FixedUpdate ()
{
if (waiting == false)
{
SendMessage("Move");
}
else
{
transform.position = Vector3.Lerp (transform.position, randomPosition, Time.deltaTime*1);
}
}

function Move()
{
randomPosition = Vector3 (Random.Range(2,2),Random.Range( 100,104 ), Random.Range( -50, -16 ));
waiting = true;
yield WaitForSeconds (moveDelay);
waiting = false;
//Debug.Log ("waited");
}
``````

Maybe you should check for the distance between the player and the bug and if it’s smaller then the desired amount, you should do a “negative LookAt”, so the bug faces away from the player then move forward some distance.
Then you should wait for some seconds, do another check with a bigger distance given, and if the player is not in that radius (ergo moved away), then you should move the bug back.