Best way to move a physics based 2D character?

Hello everybody,

up until now I always used the velocity-property on a rigidbody2D to move my character.
Now I stumbled upon this:
http://indiedevspot.azurewebsites.net/2014/09/20/physically-based-characters/

So, he says the velocity way is flawed…and he suggests to use “.position” for the movement.
Is this correct?

Thanks in advance.
I want to learn this the right way and fear I am currently movin in the wrong direction.

I don’t think his way is the best way.
I did a little test, and here’s my way to test:
Say I got a platform with a edge collider 2d attached to it, and a character to control.
When moving the character by setting velocity, the character collides with the edge correctly.
When moving the character by setting it’s position directly, the character just goes through that edge.
I’ve just started learning Unity3d, so I am looking forward to here the correct answer, too.

Thanks for the reply.
I also had the best experience when using the velocity…but, maybe this is wrong. Anybody here who is unity (2d) for a lil longer now? :slight_smile:
Thanks in advance

velocity is good for physics based character with rigidbody2d. Transform.translate, transform.position … should you use if gameobject dont have rigidbody2d.

i think, the point of the link is:
"Because it resets the velocity every frame! "

Yeah velocity is fine, if you move with transform position an object that have a rigidbody on it it will have a hard time to compensate phsyics and vector movement, and you may notice that if you have a camera following the player everything jitters.

I tried also to uses force with Impulse but is not really smooth, velocity change is the one that is more reliable for me at least.

Okay, this is all good to hear.
I was really quite confused when I found that post.

Thanks everybody.
I will continue using velocity.

Thanks again for the help.
Right now I noticed a problem with my 2D Jump n Run sandbox.

I am using the rigidbodies velocity to move the character…however, it can now get “stuck” on walls

This happens when I jump against the wall and keep the RIGHT key pressed.

My code basicly looks like this:

void FixedUpdate()
    {
        if (this.leftPressed)
            this.rigidbody2D.velocity = new Vector2(this.walkSpeed*-1, this.rigidbody2D.velocity.y);
        else if (this.rightPressed)
            this.rigidbody2D.velocity = new Vector2(this.walkSpeed, this.rigidbody2D.velocity.y);

        if (this.jumpPressed && this.canJump)
        {
            this.transform.rigidbody2D.velocity = new Vector2(this.transform.rigidbody2D.velocity.x, this.jumpStrenght);
            this.canJump = false;
        }
    }

I didn’t expect this to happen…but I kinda understand why it does happen…so, how would I fix this?
Thanks in advance :slight_smile:

Well the cheap way is to create physics materials and addthem to walls setting the friction to 0. Problem is you gonna add a lot of cilliders just for that.

THe alternative is to find a way around by calculating your own gravity and force the player to go down. A way is to raycast toward the jump direction, a short ray that when hit something in that direction stop calculating horizontal force against the wall or simply force the rigidbody down with a AddForce.

By the way a suggestion for your code, I see you call transform.rigidbody2D, by using this “shortcut” you are calling a GetComponent everytime accessing it without knowing. I would suggest to cache it at Start or Awake. Eg:

private RigidBody2D m_rigidbody2d;

void Start ()
{
m_rigidbody2d = GetComponent<RigidBody2D>();
}

And then you would just use m_rigidbody2d to access it referencing it only once at start.

2 Likes

I did as u suggested.
Depending on the horizontal speed I cast 3 rays into the given direction. Upon collision with the “solids” I reset horizontal speed and only apply vertical speed. Works good enough.

And thanks you for the reminder on GetComponent.

http://www.it-liebig.com/unity/index.html