- Home /
Enemies circle around player
I am butting my head against the wall but cannot find a way to make the enemies circle around my player. Actually, I want them to maintain distance from each other and also circle around my player ( sort of hit me and get back - like in assassin's creed). If someone can help me with the code, I will be very thankful.
I already have the AI attacking me randomly but they huddle too close together.
This is a piece of code I am using to make ai decide when to attack and when to move towards me.
if(targetalive.alive==true && lifecheck.alive==true&& !animation.IsPlaying("hit"))
{
if(distance<4&&distance>=1.5)
{
MoveTowards(moveToward, walkSpeed);
animation.CrossFade("walk", 0.1);
}
else if (distance<30 && distance>4)
{
MoveTowards(moveToward, runSpeed);
animation.CrossFade("run", 0.1);
}
else if (distance<1.5 && distance >=1.3)
{
attackRate = Random.Range(2.0,4.0);
if(Time.time > nextAttackAllowed && !animation.IsPlaying("block"))
{
doNextAnim();
timeOfLastAttack = Time.time;
nextAttackAllowed = timeOfLastAttack + attackRate;
}
}
else if (distance <1.3)
{
SlamBack (slamDirection * 1.2);
}
else
{
animation.CrossFade("idle", 0.1);
}
}
else if (lifecheck.alive==true && !animation.IsPlaying("hit"))
{
animation.CrossFade("idle", 0.1);
}
Answer by Waz · Jul 13, 2011 at 10:18 PM
It all depends on how you've structured your AI code. At any time, your AI should have a number of choices, which you weight in some manner to get your desired behaviour. In this case, you would add more weight to "move away from close allies" than to "move towards player".
I kind of understand what you are saying. I have posted some code from my AI script in my original post. Please be kind enough to give me few more hints (for circling and maintaining distance from each other)
That code is completely unaware of other allies, so of course they don't spread out.
Add code to be influenced by distance to other allies and you'll see results.
Your answer
Follow this Question
Related Questions
How to detect - as an enemy - if a fellow enemy next to you is in combat with the player? 2 Answers
Top Down Shooter Enemy AI using raycasting 1 Answer
Shooting Damage Help 1 Answer
Complete AI Tutorial? 1 Answer
2D Random AI 1 Answer