- Home /
How to make an enemy to deviate from shots?
I want to make an IA of my game . I need a way to get the shots (prefabs) in the game to compare with the enemy , and the enemy to deviate .
I've already tryed to put the prefabs in the variables on script but it didn't work. I want to get the shots and compare with the enemy position .
How can I do it?
Please I'm beginner on unity.
Answer by dr_adrii · Aug 20, 2016 at 09:43 AM
There are simple steering behaviour AI methods that you can find that will allow the unit to automatically try to avoid other objects without needing user input. This link looks at collision avoidance, but you can combine them to give more flexibility :
Combine this with functionality such as Ray cast to track the enemy projectile wrt your position, and it will try to adjust it for avoidance. You can also place a radius of movement around your unit to stop it going too far. There is a good book called The Nature Of Code by Daniel Shiffman, which explains them all very well.
Note these are very simple compared to technology we see these days, but I guess you should aim for a solution that is as simple as your needs require.
Hope it helps
Answer by thor348k · Aug 19, 2016 at 06:34 PM
By your explanation, do you mean you want the enemy to strafe away from incoming shots (bullets)? If so, are the enemies facing the player? Or wherever the shots are coming from?
If so, then you can do a raycast from the enemy, to the player. If there's a collider hit for a bullet, then strafe the enemy. You can also do a check for distance on the incoming bullet, if the bullet is say, less than 1 unit away, strafe. Otherwise, don't
Raycast: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html Distance: https://docs.unity3d.com/ScriptReference/Vector2.Distance.html
Answer by felipeRDS · Sep 01, 2016 at 08:35 AM
I've seen about ray and raycast and I got make the shoots to fire a ray.
Well, my game is this way. I'm using ray when the shots have created. Now I want that the enemy( red nave) catch these rays to move to any side.
Do I need do the verification on the same script of the shots? Because on the enemys's script I'm going to need catch the rays, but not when the game has started.
Is my idea correct? What is the best way to do it? I'm so beginner.
Your answer
Follow this Question
Related Questions
Unity 2d count collisions ontriggerenter? 1 Answer
How to get the position of a new Instantiated prefab 1 Answer
How to check where I'm being shot from. 1 Answer
help with if(hit.gameObject.tag) 0 Answers
Multiple Doors and Buttons 0 Answers