- Home /
Bumper physics not working,
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?
Answer by Marioooo · Sep 02, 2020 at 08:19 PM
Hi! I can see that you're using a solid collider, istead try using a trigger collider an OnTriggerEnter function. When you use a solid collider instead of a trigger, the collision occurs and its calculated, you're probably getting stuck because of the friction between the collider and your character collider. Change it to trigger. Hope this helps!
Your answer
Follow this Question
Related Questions
How To Make My 2D Character Jump Gradually 2 Answers
How to store remaining durability of tiles? Tilemap 0 Answers
Death Counter dont work when changing scenes 2 Answers
Trying to implement a parry mechanic 0 Answers
Virtual Joystick for Drag Indicator 0 Answers