- Home /
Facing target, then shooting
i got a cannon with slow rotation speed (using Mathf.Lerp)
and this cannon firing even if not facing foe. I can't say it something like
(if thisRotation == target.position)
//Fire
Lerp will never be right in needed position if target moving. is there any way to say my tower start shooting only if target in some cone front of itself?
Answer by robertbu · May 05, 2013 at 09:39 PM
You can calculate the angle between your target and your cannon. So if the script is attached to the cannon it would be something like:
if (Vector3.Angle(transform.forward, foe.transform.position - transform.position) < some_angle) {
// Start firing
}
Note that Vector3.Angle() returns an unsigned value.
If you are happy with the answer, click on the checkmark next to the answer to close out the question.
Answer by mowie2 · May 05, 2016 at 05:02 AM
This is what i have come up with, I know its a bit late but it helped me alot and I hope it helps others.
public bool CalculateAngle() { Vector3 targetDir = target.transform.position - gameObject.transform.position; Vector3 up = gameObject.transform.up; float angle = Vector3.Angle(targetDir, up);
if (angle < 1f)
{
return true;
}
return false;
}
Your answer
Follow this Question
Related Questions
Align GameObject to Terrain angle 2 Answers
How to Find an Object close to where the Player is Looking 1 Answer
Turret script. Making it aim in heigth only. 1 Answer
Quaternions acting up 1 Answer
Directional booster 1 Answer