- Home /
Turret Field of View?
I'm developing a space combat based game and can't figure out how to make the turrets aim at a target, but only when they are in its field of view. Yes, I have looked around and could not find anything the same as what I'm looking for.
So say I have a turret on both the top and bottom of a ship and I want the viewing angle to be 200 degrees horizontally. When an enemy is withing that 200 degree angle I want the turret to look at the enemy. The later mentioned is simple, but I can't understand a way to get a viewing angle. Thanks for the help in advance!
I'm not sure I understand what the problem is. Say your turret has a direction vector (where it "looks"). Then you calculate the vector from the turret to the enemy and check the angle between the two vectors. Do you have any problem with doing that? Or did you mean something else?
Say there is an imaginary circle around a gameObject. A circle has 360 degrees, so I need to find a way to find the degree an object is away from the gameObject and if it is acceptable, do something.
Then do as I wrote. If you need formulas for 3D geometry, you can find them on the internet, just type in keywords like "angle between vectors" or sth.
Answer by Phles · Feb 10, 2014 at 10:58 AM
Hey there, if you get the direction from the weapon to the target you can use that to get a rotation, something like this...
Vector3 targetDirection = (weapon.transform.position - currentTarget.transform.position).normalized;
Quaternion rotationToTarget = Quaternion.LookRotation(targetDirection);
Then you just need to clamp this rotation to your desired limits.
Hope this helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Simple turret script problem 1 Answer
Locking Rotation object on Z axis 1 Answer
Flip over an object (smooth transition) 3 Answers