I’m trying to create a projectile that moves in an arc, along with an arcing line renderer to help aim it, they both use the same logic and the same values but their arcs aren’t the same.
For the line renderer, I calculate an array of vector3 positions at set intervals.
private Vector3[] CalculateArcArray()
{
for(int i = 0; i <= Resolution; i ++)
{
float t = 0.2f * i;
ArcArray[i] = CalculateArcPoint(t);
}
LineStart = ArcArray[0];
return ArcArray;
}
private Vector3 CalculateArcPoint(float t)
{
Vector3 FakeForward = transform.forward;
FakeForward.y = 0f;
FakeForward.Normalize();
Vector3 HorizontalPosition = FakeForward * HorizontalSpeed * t;
Vector3 VerticalPosition = Vector3.up * VerticalSpeed * t;
VerticalSpeed -= Gravity * 0.2f;
VerticalSpeed = Mathf.Clamp(VerticalSpeed, TerminalVelocity, InitialVerticalSpeed);
Vector3 PlayerOffset = Player.transform.position + new Vector3(0, 1.5f, 0.1f);
Vector3 ArcPoint = PlayerOffset + HorizontalPosition + VerticalPosition;
return ArcPoint;
}
For the projectile, I use the same calculations in CalculateArcPoint() but in the projectile’s update method with time.deltatime instead of t.
private void FixedUpdate()
{
Vector3 FakeForward = transform.forward;
FakeForward.y = 0f;
FakeForward.Normalize();
Vector3 HorizontalPosition = FakeForward * HorizontalVelocity * Time.deltaTime;
Vector3 VerticalPosition = Vector3.up * VerticalVelocity * Time.deltaTime;
VerticalVelocity -= Gravity * Time.deltaTime;
VerticalVelocity = Mathf.Clamp(VerticalVelocity, TerminalVelocity, InitialVerticalVelocity);
transform.position += (HorizontalPosition + VerticalPosition);
}
The arcs aren’t terribly different as they do start at the same trajectory but the projectile overshoots the line renderer once the latter reaches it’s peak, which suggests that gravity isn’t pulling it down as much as it should. They both have the same gravity value so I’m guessing the issue lies with the intervals/time.deltatime.
If I double the projectiles’ gravity, it works but this feels like a band aid to a problem I still don’t understand.