Home Game Development mathematics – Unity – Inconsistent Speed Of A Projectile Traveling On A Bezier Curve

mathematics – Unity – Inconsistent Speed Of A Projectile Traveling On A Bezier Curve

0
mathematics – Unity – Inconsistent Speed Of A Projectile Traveling On A Bezier Curve

[ad_1]

What I’m doing:

I’m moving a projectile to it’s target along a Bézier curve with one control point.

The projectile moves from Transform A to Transform B with the curve created by control’s Transform‘s position. I’m using this method so that I can change how the projectile’s arc or path looks depending on the enemy’s position.

The issue:

The projectile travels from A to B successfully, however, my issue is depending on the curve the speed can be inconsistent and an unwanted ‘easing’ effect can occur.

Here’s an example in the image below. Because of control’s position, you can see the wirespheres begin to bunch up and smoosh together. As the projectile travels from A to B, it will gradually move slower as the wirespheres become closer together.

enter image description here


Here are the two scripts I am using to accomplish this:

public class QuadraticCurve : MonoBehaviour
{
    public Transform A;
    public Transform B;
    public Transform Control;

    public Vector3 evaluate(float t)
    {
        Vector3 ac = Vector3.Lerp(A.position, Control.position, t);
        Vector3 cb = Vector3.Lerp(Control.position, B.position, t);
        return Vector3.Lerp(ac, cb, t);
    }

    private void OnDrawGizmos()
    {
        if(A == null || B == null || Control == null)
        {
            return;
        }    

        for (int i = 0; i < 20; i++)
        {
            Gizmos.DrawWireSphere(evaluate(i / 20f), 0.1f);
        }
    }
}

public class ProjectileAttack: MonoBehaviour
{
    public QuadraticCurve curve;

    private IEnumerator MoveTheDart()
    {
        // Set the target
        _curve.B.transform.position = ImpactPosition.position;

        while (sampleTime < 1f)
        {
            sampleTime += Time.deltaTime * newSpeed;
            projectile.position = curve.evaluate(sampleTime);
            yield return null;
        }
    }
}

What I need assistance with:

  • I need my projectile to move along the curve from A to B with a consistent speed regardless of how the curve looks.

Thank you so much to anyone taking the time!😊

[ad_2]