[Help] AI turret's limited range (not your typical turret question)
So, how can I create an AI turret that has a limited range?
Think of the millennium falcon. It has a turret at the top and one at the bottom. The gunner on the top can only shoot to other things that are over the falcon, but as we know... the falcon does all sorts of spins and twirls, so its "over the millennium falcon" but this "over" is relative to the falcon itself.
I tried doing a very low poly half sphere model, then I attached a mesh collider and I placed that as the range of the top turret, but when you do this for many ships the performance drops a lot. Edit: I forgot to mention that I'm doing a OnTriggerEnter with this half sphere mesh collider. //End Edit
Is there anyway in code to tell the top turret to only target ships that are closer than 100 meters, but are higher than said turret BUT relative to the rotation of the falcon?
Thank you
A simple raycast from turret towards current target would suffice? Have a simple collider on the falcon that can obscure the view of the turret.
If raycast hits the falcon the target is "behind" the falcon
else if the target is still not hit (ray is 100m long) then the target has moved out of range
else shoot the target.
...or is your question more related to how you can keep the turret from pointing into the falcon and therefore limit the rotation?
@Pengocat that is indeed what I thought, but there are ships that I just want to limit the angles. Lets say that the top turret of the falcon can shoot at anything that is over the falcon (relative to the falcon). This would create a range of fire in the shape of a half sphere. But now lets say that I want the turret to only fire at anything that is over the falcon and in front of it. This would create a half sphere. Is it possible to limit the angles to get this behavior?
Thank you for your time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trouble with a State Machine for Turret 0 Answers
Aron Granberg A*, Set the AI destination setter on a prefab to follow a game object 0 Answers
Turret AI Script - one way detection 0 Answers
Smooth Rotation Quaternion Lerp? 1 Answer
Enemy watches player and shoots but bullets will not project 0 Answers