So I have an object which is moving in a circular path and enemy in the centre of this circle. I'm trying to find out how to calculate shotingDirection for bullets. Transform.position isn't ennough since bullets will be shooted in last known object position, so I need to predict/calculate object position such way, so bullets would fly in a front, so they could hit object
Suppose I have this information:
Vector3 targetPosition = transform.position;
Vector3 shooterPosition = otherTransform.position;
Vector3 rotationAxis = Vector3.forward; // axis of circle
// in counter-clockwise degrees per second around axis
Vector3 targetAngularVelocity = 90f;
Vector3 circleRadius = 5f; // in world units;
Vector3 projectileVelocity = 5f; // in world units per second;
and I want to find a Vector3 shootDirection
.
Calculate the time it will take for the projectile to intersect with the path of the target, then use Quaternion.AngleAxis
and quaternion multiplication to rotate the current direction into a predicted direction:
// INPUTS
Vector3 targetPosition;
Vector3 shooterPosition;
Vector3 rotationAxis = Vector3.forward; // axis of circle
// in counter-clockwise degrees per second around axis
Vector3 targetAngularVelocity = 90f;
Vector3 circleRadius = 5f; // in world units;
Vector3 projectileVelocity = 5f; // in world units per second;
// CALCULATIONS
float timeToIntersect = circleRadius / projectileVelocity;
float traveledAngle = timeToIntersect * targetAngularVelocity;
float traveledRotation = Quaternion.AngleAxis(traveledAngle, rotationAxis);
Vector3 currentDirectionOfTarget = targetPosition - shooterPosition;
Vector3 futureDirection = traveledRotation * currentDirectionOfTarget;
// shoot in futureDirection