Target movement prediction for projectile

I want to fire a projectile towards moving target, and to do so I need to find position to aim for.
9296816--1302608--upload_2023-9-13_3-59-44.png
Target position and velocity is known, as well as gun position and projectile speed (assume constant speed).
What is the best (performence) method to do this task?

I tried to search some resources, and there is a lot of dead links. I found two working methods, but I am unable to understand how they work. The first one look like this:

private Vector3 predictedPosition(Vector3 targetPosition, Vector3 shooterPosition, Vector3 targetVelocity, float projectileSpeed)
    {
        Vector3 displacement = targetPosition - shooterPosition;
        float targetMoveAngle = Vector3.Angle(-displacement, targetVelocity) * Mathf.Deg2Rad;
        //if the target is stopping or if it is impossible for the projectile to catch up with the target (Sine Formula)
        if (targetVelocity.magnitude == 0 || targetVelocity.magnitude > projectileSpeed && Mathf.Sin(targetMoveAngle) / projectileSpeed > Mathf.Cos(targetMoveAngle) / targetVelocity.magnitude)
        {
            Debug.Log("Position prediction is not feasible.");
            return targetPosition;
        }
        //also Sine Formula
        float shootAngle = Mathf.Asin(Mathf.Sin(targetMoveAngle) * targetVelocity.magnitude / projectileSpeed);
        return targetPosition + targetVelocity * displacement.magnitude / Mathf.Sin(Mathf.PI - targetMoveAngle - shootAngle) * Mathf.Sin(shootAngle) / targetVelocity.magnitude;
    }

I understand velocity and projectile speed check, but for example I don’t get the part with sine/cosine. Anyway the thing is it looks quite expansive even when optimized, so I wanted to do better and I think there is faster method in this article, but my little brain don’t understand the equation, or rather from where it comes exactly, why those factors are used as a,b,c, plus I am curious if there is even better method than this.

protected float AimAhead(Vector3 delta, Vector3 vr, float muzzleV)
        {
            // Quadratic equation coefficients a*t^2 + b*t + c = 0
            float a = Vector3.Dot(vr, vr) - muzzleV * muzzleV;
            float b = 2f * Vector3.Dot(vr, delta);
            float c = Vector3.Dot(delta, delta);

            float det = b * b - 4f * a * c;

            // If the determinant is negative, then there is no solution
            if (det > 0f)
            {
                return 2f * c / (Mathf.Sqrt(det) - b);
            }
            else
            {
                return -1f;
            }
        }

ChatGTP describes it this way. May not fit your scenario completely, but maybe it will put you on the right track.

using UnityEngine;

public class CollisionPrediction : MonoBehaviour
{
    public Vector3 P1; // Initial position of object 1
    public Vector3 V1; // Velocity of object 1
    public Vector3 P2; // Initial position of object 2
    public Vector3 V2; // Velocity of object 2

    public Vector3 CollisionPoint; // Point of collision
    public float TimeToCollision; // Time at which collision occurs

    void Start()
    {
        // Calculate relative velocity and displacement
        Vector3 relativeVelocity = V2 - V1;
        Vector3 relativeDisplacement = P2 - P1;

        // Calculate time to collision
        TimeToCollision = Vector3.Dot(relativeDisplacement, relativeVelocity) / relativeVelocity.sqrMagnitude;

        // Calculate collision point
        CollisionPoint = P1 + V1 * TimeToCollision;

        // Display the results
        Debug.Log("Collision Point: " + CollisionPoint);
        Debug.Log("Time to Collision: " + TimeToCollision);
    }
}

Well, I wanted real human to help me, this is why I created thread here.
ChatGPT lies, and will probably give me suboptimal or wrong solution, actually I think the code you posted is wrong (but I did not test it), and does not even solve my question as I don’t know projectile velocity as vector, I know projectile speed.

Oh boy, that allergic huh?

I am not using chatGTP for my own coding, but since I am not that familiar with all the possibilities of Unity, from time to time it helps to get a grasp of what the work flow could be. For correct code to fit somone’s own scenario you still need to be able to have some coding skills. Sorry it doesn’t work for you.

ChatGPT is great tool in general, but it’s not reliable, so I don’t like it so much, because I simply can’t trust what it says. In any case I don’t want to discuss about gpt capabilities here :smile:, and thanks for trying to help me.