0
\$\begingroup\$

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 believe v1= ac & v2 = cb. Would L be a number I pick that would decide how much it moves each frame?(almost like speed)

I imagine Length is separate from L. If so, what would Length be?

Thank you so much to anyone taking the time!😊

\$\endgroup\$
4
  • 2
    \$\begingroup\$ Does gamedev.stackexchange.com/questions/27056/… or gamedev.stackexchange.com/questions/29723/… help? \$\endgroup\$
    – Adam
    Commented Nov 23, 2023 at 1:43
  • \$\begingroup\$ Thanks for sharing these, @Adam!😊 I actually managed to find those threads earlier, but I was uncertain about a number things. It looks like in both threads the opening post might be creating their curve a bit differently than I am. I also noticed that there's multiple answers provided in the former thread. Some mentioned "approximating" the speed, but another mentioned that will not work if the curve is sharp. Forgive my ignorance, but is there a most precise method you can recommend I use? Would you be kind enough to provide me with example of applying that method to my current logic? \$\endgroup\$ Commented Nov 23, 2023 at 17:35
  • \$\begingroup\$ @Adam, I think I might be close to figuring this out, but could really use help with clarifying a few things. I believe v1= ac & v2 = cb. Would L be a number I pick that would decide how much it moves each frame?(almost like speed) I imagine Length is separate from L. If so, what would Length be? \$\endgroup\$ Commented Nov 25, 2023 at 23:01
  • \$\begingroup\$ Just to help with confustion, I believe that's how the answer provided in the mentioned above post should be interpreted: - L is the speed at which objects is moved each simulation step - length in the denominator is not a variable, but a function. So it should be read as divide L by length of vector (t * v1 + v2). Also, I am not sure why you've assumed v1 = ac & v2 = cb. These vectors are parts of curve's deriviative, and, if I am reading that correctly, should be: v1 = 2 * A.position - 4 * Control.position + 2 * B.position, and v2 = -2 * A.position + 2 * Control.position \$\endgroup\$ Commented Nov 27, 2023 at 0:30

1 Answer 1

0
\$\begingroup\$

Well, unfortunately I wasn't able to figure this out. But I didn't want to abandon this thread without closing the loop.

If you are on Unity 2022 LTS you can simply use the Unity Spline package. It does all of the math calculations for linear speed out of the box:

https://docs.unity3d.com/Packages/[email protected]/api/index.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .