Calculating Lead For Projectiles

Could anyone assist with a JS version of this script:

http://www.unifycommunity.com/wiki/index.php?title=Calculating_Lead_For_Projectiles

Thanks

Jeff

1 - Start by changing all the prefixes to postfixes separated with “:”
2 - Afterwards prefix all variables with var

Examples:

// C#
    float a = velocitySquared - shotSpeed*shotSpeed;
    Vector3 targetRelativeVelocity = targetVelocity - shooterVelocity;


// UnityScript
    var a : float = velocitySquared - shotSpeed*shotSpeed;
     var targetRelativeVelocity : Vector3  = targetVelocity - shooterVelocity;

For Function Declaration almost the same thing:

// C# 
public static float FirstOrderInterceptTime(float shotSpeed, Vector3 targetRelativePosition, Vector3 targetRelativeVelocity)
{}

// UniScript
static FirstOrderInterceptTime : float (shotSpeed : float, targetRelativePosition : Vector3, targetRelativeVelocity : Vector3)
{}

For more advanced differences somebody else has to chip in, since I don’t speak UniScript =)

Here you go. Will add the JavaScript version to the http://www.unifycommunity.com/wiki/.

// === variables you need ===
//how fast our shots move
var shotSpeed: float;
//objects
var shooter: GameObject;
var target: GameObject;

// === derived variables ===
//positions
function Awake()
{
var shooterPosition: Vector3 = shooter.transform.position;
var targetPosition: Vector3 = target.transform.position;
//velocities
var shooterVelocity: Vector3  = shooter.rigidbody ? shooter.rigidbody.velocity : Vector3.zero;
var targetVelocity: Vector3 = target.rigidbody ? target.rigidbody.velocity : Vector3.zero;

//calculate intercept

var interceptPoint: Vector3 = FirstOrderIntercept(shooterPosition,
                                                shooterVelocity,
                                                shotSpeed,
                                                targetPosition,
                                                targetVelocity);
                                                
//now use whatever method to launch the projectile at the intercept point
}


//first-order intercept using absolute target position
function FirstOrderIntercept(shooterPosition: Vector3,
                             shooterVelocity: Vector3,
                             shotSpeed: float,
                             targetPosition: Vector3,
                             targetVelocity: Vector3): Vector3
{
    targetRelativeVelocity = targetVelocity - shooterVelocity;
    
    var t: float = FirstOrderInterceptTime(  shotSpeed,
                                        targetPosition - shooterPosition,
                                        targetRelativeVelocity);

    return targetPosition + t*(targetRelativeVelocity);
}

//first-order intercept using relative target position
function FirstOrderInterceptTime(shotSpeed: float,
                               targetRelativePosition: Vector3,
                               targetRelativeVelocity: Vector3): float
{
    var velocitySquared: float = targetRelativeVelocity.sqrMagnitude;
    if(velocitySquared < 0.001)
        return 0.0;

    var a: float = velocitySquared - shotSpeed*shotSpeed;

    //handle similar velocities
    if (Mathf.Abs(a) < 0.001)
    {
        var t: float = -targetRelativePosition.sqrMagnitude
                    /(2f*Vector3.Dot(targetRelativeVelocity,
                                     targetRelativePosition));
        return Mathf.Max(t, 0.0); //don't shoot back in time
    }

    var b: float = 2f*Vector3.Dot(targetRelativeVelocity, targetRelativePosition);
    var c: float = targetRelativePosition.sqrMagnitude;
    var determinant: float = b*b - 4f*a*c;
    
    if (determinant > 0.0) { //determinant > 0; two intercept paths (most common)
           var t1: float = (-b + Mathf.Sqrt(determinant))/(2f*a);
           var t2: float = (-b - Mathf.Sqrt(determinant))/(2f*a);
        if (t1 > 0.0) 
        {
            if (t2 > 0.0)
                return Mathf.Min(t1, t2); //both are positive
            else
                return t1; //only t1 is positive
        } 
        else
            return Mathf.Max(t2, 0.0); //don't shoot back in time
    } 
    else 
       if (determinant < 0.0) //determinant < 0; no intercept path
           return 0.0;
    else //determinant = 0; one intercept path, pretty much never happens
        return Mathf.Max(-b/(2f*a), 0.0); //don't shoot back in time
}

disregard

Wonderful!!

Thanks so much - working perfectly.

Cheers

Jeff

Hi friend,

i am new to unity scripting. Can you please let me know what method i can use to launch the projectile at the intercept point?

thanks