- Home /
Issues with applying forces to a RigidBody2D
Hello, I'm developing a 2d side-scrolling platformer. I have this enemy with its usual RigidBody2D, and a script to zig zag around the platform when standing on it.
I'm trying to let the player "punch" it. So my idea is to apply a force to the object, like:
rigidbody2D.AddForce (new Vector2 (0.8f * punchForce, 0.6f * punchForce));
When I trigger that statement, the enemy jumps strangely. Sometimes it teleports to the right and jumps vertically, sometimes it jumps ridiculously fast, sometimes it jumps normally. Sometimes it jumps forwards, sometimes it jumps backwards.
I tried resetting the enemy's velocity before applying the force:
rigidbody2D.velocity = new Vector2 (0, 0);
but it still behaves erratically.
If I change the "rigidbody2D" for "player.rigidbody2D", the player jumps normally. The player has the same weight and physics material as the enemy.
Any ideas on what's going on, or how I should implement the punch? Thanks!
Answer by NaBUru38 · Mar 03, 2014 at 05:58 PM
Well, I solved it by turning off the zig zag script when the enemy gets punched, and update the ground check in the FixedUpdate:
void FixedUpdate () {
isGrounded = ...;
if (isBeingPunched && ...) {
zigZag();
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613135214im_/https://answers.unity.com/themes/thub/images/avi.jpg)