How would I be able to add in accumulating gravity to this script for parabola?

    private IEnumerator HandleParabolicMovement(Vector3 startPos, Vector3 endPos, float arcPower, float arcPowerScalar)
    {
        float timeScalar = 0;
        while (timeScalar <= 1)
        {
            timeScalar += Time.deltaTime * _moveSpeed;
            transform.position = EvaluateCurve(startPos, endPos, arcPowerScalar, arcPower, timeScalar);
            // transform.forward = EvaluateCurve(startPos, endPos, arcPoint, height, timeScalar) - transform.position;
            yield return null;
        }
        Destroy(gameObject);
    }
    public Vector3 EvaluateCurve(Vector3 startPos, Vector3 endPos, float arcPointScalar, float arcHeight, float t)
    {
        Vector3 arcPoint = Vector3.Lerp(startPos, endPos, arcPointScalar) + (Vector3.up * arcHeight);
        Vector3 startToArc = Vector3.Lerp(startPos, arcPoint, t);
        Vector3 arcToEnd = Vector3.Lerp(arcPoint, endPos, t);
        return Vector3.Lerp(startToArc, arcToEnd, t);
    }

This is something akin to a thrown grenade but I want to add gravity into the movement. After the apex/arcPowerScalar start adding more speed into its downward trajectory.

Also, I would like to know how I would convert this to steering the movement to a direction rather than a position as I want it to continue to move even after it reaches its target position. Let’s say if the player jumps and the end position is set, the projectile will just stop at where the player jumped rather than keep going in the arc until it reaches the ground.

My head explodes just trying to imagine how one would do this with lerping.

Just run the movement in Update and apply gravity as the object moves. It’s just a constant so a mere “+ gravity” suffices.

2 Likes

Nearing the end of this video Freya shows how to turn a lerp into spring dynamics.

https://www.youtube.com/watch?v=LSNQuFEDOyQ

so yeah
lol

1 Like

If you want to code your own ballistic movement it really is super-simple:

    using UnityEngine;

    public class Ballistic3D : MonoBehaviour
    {
        public Vector3 velocity;

        void Update ()
        {
            velocity += Physics.gravity * Time.deltaTime;

            transform.position += velocity * Time.deltaTime;
        }
    }

Yep, that’s it.

Now as for this comment:

You want to generally add a significant fraction of the player’s velocity to the launch velocity.

So let’s say your toss velocity is Vtoss

And your player is jumping at Vjump

Easiest is to just combine them straight up:

Vector3 VfragOut = Vtoss + Vjump;

and assign VfragOut into the velocity field above when you Instantiate() the thing.

2 Likes

The code in the first snippet doesnt seem to care about where its going to end up. Would I need to have some kind of calculation to determine how much velocity is needed to be applied in order to get to that target position?

What I like about the code in OP is that it allows me to adjust when and where the arc/apex occurs. Sometimes there are objects that need to be arced over like tables and such so it allows me to have more control.

Yes. You can find formulas for that kind of thing if you search for “projectile motion”.

https://phys.libretexts.org/Bookshelves/University_Physics/Physics_(Boundless)/3:_Two-Dimensional_Kinematics/3.3:_Projectile_Motion

Edit: Apparently Wikipedia has an entire article for determining the range of a projectile. That’s so random.

https://en.wikipedia.org/wiki/Range_of_a_projectile

The crazy part of my brain immediately thought of using the Splines package, but the sane part of my brain smacked it and told it to find the closest thing to a cheat sheet of formulas which is what the above link is.

https://docs.unity3d.com/Packages/com.unity.splines@2.6/manual/index.html

2 Likes

Well… to be fair, one of the first serious uses of computers was to compute artillery trajectories. :slight_smile:

That used to be done by “human computers” using slide rules. Try googling “human computers”… pretty nutty stuff.

2 Likes

lol until Coriolis effect knocked their abacuses and slide rules off the table. Fun fact: Mr. Coriolis died 70+ years before the popular usage of the term “Coriolis effect”.