- Home /
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");
}
Do you need a raycast? Isn't this just a calculation based on the player's position and the bug's position?
I'm not sure, I assumed i'd cast a ray, and if the distance between bug and player is less than 1, move out of the way.
Well I think @Bazsee has the right idea in the answer he posted after I made the comment. I would have thought just adjusting the position to maintain the $$anonymous$$imum distance using the current vector between the player and the bug would give the effect you are looking for (though you'd have to watch for bugs overlapping each other).
That approach would work well if the bugs look ok moving at the speed of the approaching player or slightly faster.
Answer by Bazsee · Jun 12, 2012 at 11:23 AM
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.
Now, i got that working, thank you. Too your advice, however do you have any suggestions so that I can cast a ray in all directions, ins$$anonymous$$d of single direction? That;'s my only hurdle now aha
Your answer
Follow this Question
Related Questions
How to Stop Enemy "Shooting Through A Wall" 1 Answer
enemy raycast to detect player 1 Answer
Enemy raycast detection 0 Answers
Enemy AI Script Acting Abnormal 0 Answers
AI script works, when we add a blocked function it stops working HELP 0 Answers