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!
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\$