WiggleWizard/quake3-movement-unity3d

Issue implementing speed limit

Closed this issue · 0 comments

I have been trying to implement a max speed that the player is not able to exceed, my logic seems correct but for some reason the player will sometimes exceed the limit and then proceed to accelerate so fast and go so far that it will crash the unity editor.

This is currently how im implementing the max speed.

private void Accelerate(Vector3 WishDir, float WishSpeed, float Accel)
{
        float MaxSpeed = 12f;
	float AccelSpeed = Accel * Time.deltaTime* WishSpeed;
	float CurrentSpeed = Vector3.Dot(PlayerVelocity, WishDir);
	float AddSpeed = WishSpeed - CurrentSpeed;

	if (AddSpeed <= 0)
		return;
	if (AccelSpeed > AddSpeed)
		AccelSpeed = AddSpeed;

	if (CurrentSpeed + AddSpeed > MaxSpeed)
	{
		AccelSpeed = MaxSpeed - CurrentSpeed;
	}

	PlayerVelocity.x += AccelSpeed * WishDir.x;
	PlayerVelocity.z += AccelSpeed * WishDir.z;
}

I have not clue where i am going wrong here any help or insight is greatly appreciated!