0
\$\begingroup\$

Good Evening All,

I am moving my projectiles in a parabolic arc using the following code:

(Credit to Joe Strout for the formula! https://luminaryapps.com/blog/arcing-projectiles-in-unity/)

public IEnumerator JumpInAnArc()
{

    Vector3 startPos = GameEngine.Instance.BattleManager.TurnHero.transform.position;
    Vector3 targetPos = GameEngine.Instance.BattleManager.TargetEnemy.transform.position;
    float jumpSpeed = 3;
    float arcHeight = 3;
    Vector3 nextPos = Vector3.zero;


    

    while (transform.position != targetPos)
    {
        
        // Compute the next position, with arc added in
        float x0 = startPos.x;
        float x1 = targetPos.x;
        float dist = x1 - x0;

        

        float nextX = Mathf.MoveTowards(transform.position.x, x1, (jumpSpeed) * Time.deltaTime);
        float baseY = Mathf.Lerp(startPos.y, targetPos.y, (nextX - x0) / dist);
        float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist);
        nextPos = new Vector3(nextX, baseY + arc, transform.position.z);

        transform.position = nextPos;
        
        yield return null;
    }


}

This works great, but the speed is consistent throughout the objects entire movement. Now, I would like to add weight/velocity to the object's vertical movement on the Y axis. I would like the projectile start fast and begin decelerating it's Y velocity as it travels up in the Y axis. I'd like it to slowly taper it's velocity to 0 when it reaches it's peak height. Then, I would like it to start accelerating as it falls down towards it's target.

Is anyone able to assist me with how I can accomplish this? Thanks for taking the time!

\$\endgroup\$
5
  • \$\begingroup\$ The code you've shown already does this. Take the derivative of the arc variable with respect to time and you'll see it starts positive, and ends negative, and is continuous in between - meaning it must decelerate, cross zero, and accelerate downward. Do you not observe this acceleration in the arcing of your projectile's trajectory? \$\endgroup\$
    – DMGregory
    Sep 28 at 0:50
  • \$\begingroup\$ Thanks as always for the reply, Doug! Huh, that's interesting...I don't notice much of a difference in speed. Maybe it's just a very small amount? I also use this formula for my character's jump attack. Here's an example of the code in action and what I'm seeing: youtu.be/7kbM9fDQHSc \$\endgroup\$ Sep 28 at 1:19
  • \$\begingroup\$ You can see this just from the shape of the jump. Since x is changing at a constant rate, if y were also changing at a constant rate you'd get a straight line. The only way to get an arc shape is for y to start fast, slow down, stop, reverse, and speed up. You're currently using a rather low height for a jump of this duration, which makes it look like moon gravity, with very gentle acceleration. So you could enhance the effect by shortening the time or increasing the height. Or you could break with real-world physics and use a cubic or quartic curve instead of quadratic. \$\endgroup\$
    – DMGregory
    Sep 28 at 2:03
  • \$\begingroup\$ Good Morning! Oh, goodness, you're right. It would be a straight line in that case. When you explain it that way it makes perfect sense. Hmm, I made the arc higher and increased the speed, but it still reminds me of "moon gravity", like how you put it. Here is an example of what I'm seeing: youtu.be/iOo6dUJDysI Is there a good way to to improve this effect and make the change more dramatic and more satisfying? Thanks! \$\endgroup\$ Sep 28 at 15:52
  • 1
    \$\begingroup\$ This looks like something I would do with the animation system instead of doing it in code. Animators are not just for animating 3d characters. You can use them to control any changeable inspector property, including position and rotation. \$\endgroup\$
    – Philipp
    Sep 30 at 7:41

0

You must log in to answer this question.

Browse other questions tagged .