Hey there
I have a fairly simple speed pad in my game whereby if the player is passing into Collision box of the pad, the player is given speed in the direction they are traveling. So it’s omni-directional. Now this have created a few issues because if I place pads next to each other to build up some speed, the ball can travel so fast that it starts bypassing collision checks.
So I thought “Put a threshold on the pad” so if the ball is already at a fairly high speed, don’t let it speed up any further if it passes over another speed pad.
It doesn’t really seem to work as the ball still reaches quite high velocity. My logic is probably off:
void OnCollisionEnter2D(Collision2D collisionInfo)
{
if (collisionInfo.collider.GetComponent<Rigidbody2D>() != null)
{
Rigidbody2D rBody = collisionInfo.collider.GetComponent<Rigidbody2D>();
float currentXSpeed = rBody.velocity.x;
float currentYSpeed = rBody.velocity.y;
float newXSpeed = currentXSpeed + SpeedMultiplier;
Vector2 newVelocity = new Vector2(currentXSpeed, currentYSpeed);
if (currentXSpeed > 0)
{
if (newXSpeed < UpperSpeedThreshold)
{
newVelocity.x = newXSpeed;
}
}
else
{
if (newXSpeed > LowerSpeedThreshold)
{
newVelocity.x = newXSpeed;
}
}
Debug.Log("Velocity: " + newVelocity.x + "," + newVelocity.y);
rBody.AddForce(newVelocity, ForceMode2D.Impulse);
}
}
The multiplier is set to 1.0f. I reach velocities of +10 some times and -4 going the other way even though technically this shouldn’t be possible.