x


Make AI run from Player

I have an AI the relies on Force to move, but I need it to flee from the player's position once it reaches a certain distance. Normally this wouldn't be a problem, but once they hit a wall, they get stuck and using OnCollisionEnter doesn't help since they will force themselves towards the wall until they get locked. The game runs in 2D with the Z-axis locked. I tried the bounce material with similar results and raycasting is out of the question.

Question: How do I get the AI to flee from the player, but go the opposite way once it hits the wall, without touching the player?

var FromPlayer: Vector2 = Target.position - transform.position;
if(FromPlayer.magnitude < detectionRange){
rigidbody.AddForce(1,2,1, ForceMode.Impulse); 
    //rigidbody.velocity = -transform.forward * 2;

    if(transform.position.y == Target.position.y){
    rigidbody.AddForce(0, Random.Range(-1, 1), 0, ForceMode.Impulse);
    }

    if(FromPlayer.magnitude < 2){
    rigidbody.AddForce(transform.forward * 1, ForceMode.Impulse);
}



function OnCollisionEnter(other: Collision){
if(other.gameObject.CompareTag("Wall")){
rigidbody.AddForce (Random.Range(-2, 2), Random.Range(-1, 1), 0, ForceMode.Impulse);

    }


}
more ▼

asked Mar 26 '11 at 11:04 PM

Persona gravatar image

Persona
291 78 88 100

I just posted a script that might work well for you. Only thing is that they stop when out of range and start running again when you get into range, But I'll bet that can be tweaked though.

LINKY:

http://answers.unity3d.com/questions/42238/an-ai-that-will-move-away-or-move-to-another-waypoint-when-player-goes-near

Mar 27 '11 at 02:41 AM Michael 12

Sorry, but no character controllers are used for it.

Mar 28 '11 at 02:21 AM Persona
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Hi,

Just one advise for checking the distance with the playes, take a look to colliders, specially to the OnTriger...() functions .

Then with the wall staff, I'm trying to figure out that what you want to get is just a constant minimum distance respect the player, so why not OnTriggerStay?

If you it is not exactly what you need, please give me more details.

Cheers,

more ▼

answered Mar 26 '11 at 11:51 PM

Uzquiano gravatar image

Uzquiano
510 22 26 35

The idea is to avoid the player's collider by stopping and reversing direction, trigger or not, otherwise they are destroyed.

Mar 28 '11 at 02:20 AM Persona
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3657
x2698
x1872
x1254
x418

asked: Mar 26 '11 at 11:04 PM

Seen: 1910 times

Last Updated: Mar 26 '11 at 11:04 PM