- Home /
Character flies away at high speed with Rigidbody.AddForce
I am working on a game similar to Super Smash Bros. In this game, characters need to be able to receive knockback of different angles. My code appears to work when using this to launch my character straight upward, but trying to use it to launch any other angle, even 1 degree off, it simply zips my character off the screen at ridiculous speed. I have a screen recording of the phenomenon as well as the relevant code.
Video: link text
About a quarter of the way through is when I attempt to use it to launch me upwards, and halfway through is to the side.
Code: This 'zipping off the screen' result occurs when either attempting to hold left or right to DI this attack, or when I edit the numbers so the attack sends me in another direction
I still have no idea what's going on. I've tried changing the 'transform.up' to 'transform.forward', and that simply has no effect (presumably due to the fact this is a Rigidbody2D) and to 'transform.right', and that has the same 'zipping away' result. I've tried rotating the launch helper manually ins$$anonymous$$d of with code, but that gave the same result. I'm not sure what else I can try.
Answer by Eno-Khaon · Nov 23, 2018 at 09:48 PM
First off, looking at how you're applying the force, you should probably change the ForceMode2D to Impulse. Among other reasons, this will let you reduce the force application by 50 times (by default), as well as allowing you to change the physics timestep without changing the amount of force applied.
// multiplied by mass as an example to ignore mass as a factor for the force application
rb.AddForce(forceAmount * rb.mass, ForceMode2D.Impulse);
Seeing that you're currently applying a force of 50,000 (reduced to 1,000 instantaneous by the default ForceMode at the default physics rate of 50/s), however, this brings to light other questions and concerns.
The reason your character flies off to a side at incredible speed is because you're suddenly sending them to the side at up to 1,000 units (meters, etc.) per second. Therefore, the more urgent question here is: Did you turn gravity waaaaaaay up or are you applying downward force by some other, additional means?
Hey, thank you for getting back to me. So, I looked into this, and for testing purposes, changed the actual launching line to:
rb.AddForce(launchHelper.transform.up * 10, Force$$anonymous$$ode2D.Impulse);
In addition, the gravity is the normal -9.81, the mass of the character is 1 (hence why I didn't bother multiplying by mass), the RB2D's gravity scale is also 1, and it doesn't look like any other external sources are making it go downwards at speed. To top it off, using the code to launch it straight downwards did so at around the same speed as the one straight upwards. Even after these changes, launching straight up and down works fine, but trying to go any other direction, even 1 degree off, is still causing the object to fly away at huge speed. In fact, even making the code launch me at "launchHelper.transform.up * 0.1f" still causes this 'zipping away effect'. I can't tell if the power of the AddForce is what's causing this effect or not.
Looking over the gif again, it occurs to me that, if I'm understanding what I'm seeing correctly with the fairly low framerate, the character appears to accelerate as it zips away.
That could suggest interference from another script applying velocity to the side, since your script sample doesn't appear to have any specific reason to rapidly call the function listed.
To test your theory, I disabled all scripts attached to my object, and it turns out, it was actually something from my jump script messing with it! I figured out what line it was, and managed to completely fix it! Thank you so much!
Essentially, I had it on every frame doubling the sideways velocity for some reason. Glad it got figured out.
Your answer
Follow this Question
Related Questions
AddForce to mouse direction not working well horizontally. 1 Answer
Rigidbody.addforce not working with prefab 1 Answer
Array of different rigidbody 0 Answers
rigidbody2d.addforce for is not working for the x axis 0 Answers
2D AddForce towards direction with CONSTANT FORCE whether the direction is far or near. 1 Answer