- Home /
following enemies simple AI problem
I'm using the most simple AI for my enemies, but it has some problems. Enemies follows the character and after some time they goes to one place. I don't know how to explain it, so I made a simple scene, here is the video(imagine spheres are enemies): http://img201.imageshack.us/img201/6026/e1jkgkfvdbucbsqnfrxmcn.mp4(sorry for poor quality). Here is the code:
public GameObject characterObject;
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, characterObject.transform.position, 3 * Time.deltaTime);
}
any ideas how can I avoid that? I would like to fix it without rigidbody. Hope someone will help :).
I believe the best fix is to use rigidbody, make it kinematic. An other fix may requires a lot of work.
Seeing how you are looking for an efficient way to do it, that really depends on what game you are trying to make. Physics/Collisions are very efficient, you'd need a very specialized solution to beat them.
So... what game are you trying to make?
it's like top-down shooter, very similar to $$anonymous$$igore.
Answer by nsxdavid · Feb 24, 2013 at 05:45 PM
There is no bug, per se. It is doing what you told it to do. Every enemy is trying to reach the same point, moving at the same speed. That point moves, but still they are all essentially going to always end up on top of each other.
To avoid it you need more more variables in the system. For example, varying speeds of enemies might help some. But beyond that, you probably want some sort of steering behavior to try and keep them apart.
You might want to check out UnitySteer on github for a solution.
did I said it's a bug ;)? I know it's not, I just can't think the solution without using rigidbody. Game is for android devices, so I'm searching maybe more efficient way than rigidbody. What is UnitySteer, any related examples :)?
UnitySteer are tools for making more intelligent behaving AI. Steering behavior. For example, one thing it can do is keep AI actors from getting too close to each other as they chase some goal (aka your player). Which is precisely what you are after, I think. Its free too.
If you use UnitySteer, could you help a little bit here? I download examples but it can't find some required scripts. Where to get these scripts?
Answer by fafase · Feb 24, 2013 at 07:05 PM
Most simple way would be to go with:
public Transform player;
public float speed;
void Update(){
transform.LookAt(player);
transform.Translate(0,0,1*speed)
}
If you feel like learning basic of NPC AI you can have a look there: http://unitygems.com/basic-ai-character/
it's actually exactly the same code which I posted above. I need an efficient solution for problem which I showed in the video.
"They go to one place" do you mean when they start to get on top of each other? If so, it is because you are moving them with the transform. If you want them to stick individually, use the Character Controller and move them with it. (The link I provided has all about that)
yes, I mean "they start to get on top of each other". But would character controller component be efficient for about 10-20 enemies in scene at the same time?
Yes. The other way would be to constantly check for distance between each object which then would get quite expensive while CC is all doing it for you. Actually, most game use CC and they go well with it.
If I were you (and I am not) I would try and see, if that is not doing it then you may think of another way but I am quite confident it will be fine.
Answer by KingKongFu · Feb 24, 2013 at 07:40 PM
You could just stop them a bit away from the final destination but that would still keep them piling up one way is to check if there is something in front of the bot using ray trace and if there is stop the movement. That would not add much complexity to the game and keep overhead down to a minimum. I hope this help.
I already thought about distance checking of closest enemy, but I'm not sure if game will look interesting if all enemies will be like "start stop start stop".
well it seams to me that you want like a zombie mob thing so what I was thinking is that they basically stack up on each other so that would not look bad. They would just get in each others way.
Your answer
Follow this Question
Related Questions
AI navigating all 6 sides of a cube 0 Answers
Simple AI and Animation 2 Answers
How to get a velocity unit vector from a NavMeshAgent? 1 Answer
AI in Untiy, best practices 1 Answer
Pong game AI problem. 1 Answer