- Home /
Best way to move a character in a 2d platformer?
transform.translate causes a steady movement but causes the player to jitter.
rigidbody2D.velocity=Vector3.right* -4; makes the character move steady but makes gravity work weird. If you are running then try to jump, it barely works. But if you jump then move, you can glide for a long distance
rigidbody2D.AddForce (Vector3.right * 4.5f); starts off really slow but within a few seconds is moving too fast too control.
What is a good way to get my character to move properly without the negative effects of the three examples that I mentioned? If more info is needed, I will try to provide it.
Plus if I use transform.translate, setting timeScale to 0 doesn't work right. As anything using transform.translate still moves.
Oh I think I have an idea, I just don't know how I would do it. Can I set a limit/maximum to the force applied if I used the addforce method?
Answer by Anxo · Jan 02, 2015 at 06:07 PM
transform.translate does not cause jitter, the jitter is caused by what and how you are feeding the method. All 3 of these are fine, it just depends on what kind of controls you wish to accomplish. velocity is the easiest and most direct way to control your character while still respecting collisions and physics, the problems you are running into with gravity is just due to how you are applying it.
For instance, cushion your moving velocity in a "isGrounded" bool. That way you control the velocity while running but let physics take over once you jump.
run, velocity is in control, hit jump, isGrounded = false, physics takes over and controls the character till isGrounded is true again.
But just do this tutorial and you will get the idea. http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
I was using a isGrounded bool already to stop the player from jumping while in the air, but with using the method that you mentioned would I be able to allow to player to somehow change directions in the air? I want the player to be able to correct themselves slightly if they time their jump was timed incorrectly. Plus I just tried it and it works fine only if I left off the movement key first. Should this mean that I should put jumping over movement in the script? Going to watch the tutorial as soon as it loads. Slow internet and that's a long tutorial ^_^ Thanks for the reply btw
I just thought about it for the movement in the air, I could just do an if check to see if they are grounded then if not, they can move, just at a slower rate. I think thats right anyways
yes, if you wish to manipulate the trajectory of someone in the air you can just ad to the velocity.
rigidbody2D.velocity = rigidbody2D.velocity + airControl; // air control being a vector 2