# How to calculate angular velocity of target relative to player object

I am working on a game that has some vehicle weapons that will track locked on targets. To add some realism, I am making it so that the angular velocity of the target (relative to the player/attacker) affects the success rate of the automated weapon.

Let us assume for right now that the speed of the projectiles the attacker is shooting at the target is not important for our calculations (consider instant for now)… only the ability of the gun to swivel fast and accurately enough to keep up with the target is important for now. Also, let us ignore how fast the target is moving towards or away from the attacker.

Additionally, this need to take into effect the rotation of the player as well. If the player turns away from the target as the weapon is trying to home in, this should add to the relative angular velocity of the target. Also, just to be clear, we are looking for how fast a target APPEARS to be going relative to the attacker, not actual velocity (far away would appear slower, point blank would appear much faster).

I am new to 3d game design and this is a little outside of my skill level. Thank you for any assistance you can give.

TL;DR - Need to calculate perceived angular velocity of target relative to player angular velocity.

For any given frame, within a script of the player, you can calculate the current normalized player-to-target vector, and compare with previous frame’s vector.

For example, declare:

``````private Vector3 actualPlayerToTarget = Vector3.zero;
private Vector3 previousPlayerToTarget = Vector3.zero;
``````

Within an Update loop, supposedly you already have a game object tracked, let’s call that targetObject, then you can calculate the angle between those two vectors, giving you your angle difference:

``````actualPlayerToTarget = (targetObj.transform.position - transform.position).normalized;
float angleDifference = Vector3.Angle(previousPlayerToTarget, actualPlayerToTarget);

// TODO: Use angleDifference, perhaps compare to maximum difference allowed for hit probability or such

previousPlayerToTarget = actualPlayerToTarget;
``````

AFAIK angleDifference will be in radians, keep in mind.

From there you can devise a formula that will reduce the hit probability with larger angle, or simply compare if it’s less than maximum gun rotation per frame to see if you can hit the target.