Confused about how AddForce works
Hey there, guys! I wanted to check out something with the community, because I can't seem to get why add force works the way it does (I assume it has something with gravity, but it will be way easier for me if I understand why).
Anyways, here is the problem- I try to make a 2D platformer, and for jumping, I use a simple one line of code- if jump button is pressed, the code adds force equals to Vector2.up * jump velocity. And it works like a charm. However, if I substitute Vector2.up with Vector2.right, it does nothing, or if the velocity is some insane number like 42000, it teleports the player few pixel to the right.
I did solve this, in the end, because I wanted this for my dash script, by implementing a timer and a bool, so I add horizontal force over some amount of time (the timer parameter), and resetting it after it reaches zero (plus cooldown, but that's not really relevant to the question).
So, actually, I'm completely confused about why is this happening? Why the simple one line of code for adding force on the Y axis gives a smooth jump, and I need all the timers, bools and ten times more complex script for achieving the same on X axis? I assume it has something to do with gravity, but I'm really confused as to why exactly this is happening.
You should show the line of code. Edit your post to add it. Without it, we can only assume what you are doing wrong.
Answer by Firedan1176 · Apr 26, 2016 at 03:20 PM
For the jump, I think it would be more beneficial if you are also including ForceMode.Impulse
in your AddForce call. However, I do not understand what is causing the problem for your x-axis movement. Is it possible you are moving away from the camera in the Z axis (in 2D) instead of the X axis? Other than that, I do not know what could be causing the problem without seeing your code AND your scene.
Your answer
Follow this Question
Related Questions
Rigidbody doesn't seem to apply gravity 1 Answer
How to make GameObject fall with the same gravity while using AddForce() 1 Answer
How to make a object jump constantly at y and move to the next position to z (perfectly) 0 Answers
How to change physics properly? 0 Answers
Character not jumping smoothly using CharacterController 0 Answers