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
1 Answer
1
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?
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 instead of with code, but that gave the same result. I'm not sure what else I can try.
– Nackles42