Hi guys, I’m starting a 2D platformer and when implementing a bumper-type obstacle I encountered a problem. The way the bumper is supposed to work is by detecting a collision with another object in OnCollisionEnter2D(), finding the contact point and normal, then adding an Impulse force to the opposite direction of said normal.
It works fine for vertical bumps, but when hitting the bumpers with horizontal motion, the player just kind of sits there. Here is the code for the bumpers:
private float bumperForce = 10f;
void OnCollisionEnter2D(Collision2D col) {
ContactPoint2D[] otherObjects = col.contacts;
foreach(ContactPoint2D contact in otherObjects)
{
var norm = contact.normal;
col.rigidbody.velocity = Vector2.zero;
contact.rigidbody.AddForce( -1 * norm * bumperForce, ForceMode2D.Impulse);
}
}
And here is my movement script (the important part, at least):
void FixedUpdate()
{
// Jump force
if (jump)
{
PlayerRigidbody.AddForce(new Vector2(0f, JumpForce), ForceMode2D.Impulse);
jump = false;
}
// Move the character by finding the target velocity
Vector2 targetVelocity = new Vector2(horizontalMove * speed, PlayerRigidbody.velocity.y);
// And then smoothing it out and applying it to the character
PlayerRigidbody.velocity = Vector2.SmoothDamp(PlayerRigidbody.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
}
And here is a short video of the bumper in Play mode:
Any ideas?