- Home /
Making rigidbody force movement snappy
I am making a platformer but I want the player character to have a rigidbody so he can correctly collide with other rigidbody objects in the game world.
After transform.Translating the character box (just a placeholder gameObject) I saw that was not the way to go and started AddingRelativeForce to the character (for movement AND for jumping).
However, I ran into multiple issues:
The movement starts off sluggishly
The character jump is not snappy and responsive, but looks and feels as though he were a rocket that's slowly lifting up
After the character finally picks up speed he quickly becomes uncontrollably fast
How can I achieve a responsive and accurate movement of my rigidbody character? I want the game to feel like Super Mario Bros and not like Little Big Planet (both great games, mind you). Thanks.
Have you looked into using a CharacterController? Just had to make sure you considered that first.
Adding force to an object over time (in FixedUpdate()) will continuously increase the object's velocity until you stop adding force.
You could add force once on move button down, then add the negative value of that force once when the movement button is lifted up. Add force once to start movement, the add then negative force once to stop movement (in that direction).
Same for jumping. Add a force once to propel it upwards, and then it will fall back down. It will jump higher or lower depending on the amount of initial force used.
When using this method with AddRelativeForce (or AddForce), make sure the Force$$anonymous$$ode is set to Impulse.
Answer by 2d4Games · Aug 19, 2013 at 08:49 PM
rigidbody.addForce and addRelativeForce are acceleration modifiers, that slowly increase speed. What you want to do is set rigidbody.velocity to a value, as that will produce instant movement and it won't be sluggish or speed up unexpectedly. Also, when setting the velocity vector, remember to have a scalar multiplier on it so you can easily control the speed.
Something like:
var speed = 10; //This is a multiplier that you can easily tweek in unity.
rigibody.velocity = Vector3(0,1,0) * speed; //multiply the direction by an appropriate speed, Vector3(0,1,0) is straight up.
can you use velocity in such a way that it is relative to the rigid body?
Take the transform.forward vector (which is normalized) and multiply it by a scalar (what you want the velocity to be in that direction, i.e. the direction the object is facing, the forward z+ direction)
Thanks for the answers so far. Your solutions work perfectly but I should have given you more info to see why they do not apply to my particular issue.
The characters in my game have Jedi-like force push abilities (this could be anything, from AddExplosionForce to the new particle collision callback implementation) and I want them to be able to push each other. If I set velocity and/or position directly weird things start happening (for example on a horizontal push all energy gets transferred to Y axis and they fly straight up).
I tried using AddForce but the implementation I found increased drag which was proportionate to character vector magnitude. This worked fine as well but the drag was largest when there was no input (to prevent the character from sliding around when you released the analog stick) which meant the character could not jump because of too much drag.
Changing the position and changing a rigid body's velocity are not the same. You shouldn't manipulate the position of a rigid body directly, or you will get unexpected results, because you are cheating the physics engine.
I'm not sure why changing the rigidbody.velocity is giving you unexpected results. I should mention the using AddForce with Impulse as the Force$$anonymous$$ode essentially does the same thing as changing the velocity directly, except your input is a force ins$$anonymous$$d. $$anonymous$$ight be worth to try that out if you haven't yet. All depends on how you are deter$$anonymous$$ing the movement of your characters which would be easier or make more sense.
Okay, would this be the correct way of handling rigidbody movement for my case?
transform.rigidbody.velocity = new Vector3(movementDirection.x * movementSpeed, transform.rigidbody.velocity.y, 0);
Answer by Baintastic · Nov 25, 2015 at 12:18 PM
Editing your velocity directly works great if there are never any external forces in your game. Otherwise, set the drag in your rigidbody and use:
rigidbody.AddForce(force, ForceMode.VelocityChange);
Answer by scipiothegreat · Aug 20, 2013 at 01:24 AM
The best way to handle is this is to use
rigidbody.AddForce(force, ForceMode.Impulse);
The default ForceMode is ForceMode.Force which applies a certain amount of force over the space of a second. ForceMode.Impulse applies the same amount of force in one physics frame.
I am already using this for the jump and it works okay. What about sideways movement?
There is also Force$$anonymous$$ode.Accerlerate which increases the rigidbody's velocity without regards to the mass.
Answer by orbitopus · Aug 19, 2013 at 08:55 PM
I'm new to unity and am working on similar physics movement and came across something that might help
for snappy jumping you can use Rigidbody.velocity to add a burst speed in a direction
if (Input.GetKeyDown("Jump")){
rigidbody.velocity = Vector3(0,10,0);
}
let me know if you came up with a solution to any of your other problems. :)
This one works fine (I just tested it) but I am afraid it does not apply to my particular case. Check my bottom comment if you wish :).
I will be sure to inform you if I find a solution to my problem
Okay, this series of videos looks quite promising: https://www.youtube.com/watch?v=GddQmaFLZ00&list=PL7AE076AFAFD3C305