- Home /
enemy reacting to player
I'm trying to make an enemy know when to dodge and/or block an attack from the player, and I'm not sure how to start, I suppose using ray/line cast to see if the player is in sight but beyond that not too sure. If someone could point me in the right direction on how to do that, that would be great. I'm working in javascript but pseudo code is great
A little more info might help. Do you mean a physical or a projectile attack? Do you want the enemy to stay in the same area or do you want them flee, zig-zag, etc.? There are a lot of conditions that you may have clear in your $$anonymous$$d that anyone trying to help you doesn't know. Also, what part of this process is causing you trouble? Detecting player attacks? Triggering an enemy response? Coding the movement algorithm?
Try breaking down your intended action into separate steps: Step one, is the player firing at me? Step two, deter$$anonymous$$e the direction of the attack. Then ask for help...
sorry about that, I would like the enemy to dodge projectile fire, and take cover. Coding the movement I know how to do, but detecting player attacks I'm unsure about. hope that clears up my question, I have a problem of having something in my head and not transferring it to other mediums.
Answer by The_r0nin · Dec 01, 2010 at 02:41 AM
You could check the distance from the player to all enemies (use FindGameObjectsWithTag and then a "for - in" to iterate through the enemies) on each gunshot and have the enemies react to the "sound." Or, you might want to :
if (gunshot){
var enemiesList: GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
for (var enemy: GameObject in enemiesList){
if (enemy.renderer.isVisible){ // you might have to do a GetComponent to find the renderer, depending on your hierarchy
// program your evasion routine here, or send a message to that enemy to evade.
}
}
}
This will make only the enemies in your camera (if you have only one camera... it will return "true" for every gameobject visible in ANY camera) react to the gunshots. This will simulate the enemies you are shooting at knowing that bullets are flying nearby... but the rest of the enemies will behave as normal (so you won't have enemies behind you running for cover).
Your answer
Follow this Question
Related Questions
How to make 2 groups of agents attack eachother? 0 Answers
Making a fighter respond to being attacked 1 Answer
Rotate with Raycast??? 2 Answers
Following AI - Similar to Snake Game 2 Answers
Unity AI FPS problem 0 Answers