Search Unity

How to move properly a sprite (character)

Discussion in '2D' started by Denbrough, Nov 24, 2013.

  1. Denbrough

    Denbrough

    Joined:
    Nov 24, 2013
    Posts:
    4
    Hello!

    I'm trying to develop a platform game and I'm wondering how to move properly a sprite.

    I read some tutorials and recommended use the same as 3d. move directly the sprite. but I read the box2d documentation and says that is not recommended because can cause some trouble with colissions, they recommend using AddForce. But when I use addForce I get some extrange behavior, (used the controller of the potato tutorial) my sprite slips over the floor, tried to add some friction got the same result.

    Thats why I'm wondering what method must be used. There so little documentation about this.

    Thanks and sorry if the answer has been answered, but I read a lot of this and in some cases transform is used in other cases addforce.
     
  2. Tiggster

    Tiggster

    Joined:
    May 22, 2013
    Posts:
    29
    I'm just a beginner myself, but I personally use them both. Translate seems to work fine, but AddForce takes more advantage of the 2d physics for things like jumping. It will be nice when more in depth tutorials are available for the new Unity 4.3 2d functionality.
     
  3. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    I haven't worked with 2D physics in Unity, but I have work with an implementation of box2d before. To get consistent results, you need to apply an instantaneous force. This is different than a normal force that is applied over a length of time. You should also be able to change the velocity directly.

    For movement, you could set a constant velocity.

    For jumping, you could either apply an instant force or set the velocity up.

    Those will lower the chance of a physics glitch occurring, like clipping through the floor.
     
  4. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    It might help too, if you manually set the velocity to zero when you want you character to stop moving in a particular direction.

    To make the character stop when the player releases the move button, set the x velocity to 0. Or, Set the x velocity to a lower number and allow friction to bring the character to a halt.

    There are a lot of different things you could do and there is no right way.

    Hope this helps.
     
  5. Denbrough

    Denbrough

    Joined:
    Nov 24, 2013
    Posts:
    4
    It really helps. Thank you both I will try use forces for jumping and things like that and us and use velocity to move the character, if I see some kind of bad behavior (like you said clipping, ocured to me in LibGdx with Box2D) I will try the other solution, addForce and stoping the character when the button is released.

    Thank you both the help.
     
  6. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    After digging deep into it, it looks like all forces in the 2d physics are instantaneous right now. You should use FixedUpdate() when working with physics.

    The best model for side to side movement is probably to apply a force while the character is moving and set a max speed. And then when the charcter stops getting move commands, apply a force opposite and proportional to the horizontal velocity with a minimum force. In the real world, when you stop walking, you have to physically stop yourself, or you will keep moving and fall over. I think this will give you a great amount of control and seem very natural.
     
  7. Denbrough

    Denbrough

    Joined:
    Nov 24, 2013
    Posts:
    4
    I've been usingthe controller that the tutorial uses:
    if (h * rigidbody2D.velocity.x < maxSpeed)
    rigidbody2D.AddForce (Vector2.right * h * moveForce);

    // If the player's horizontal velocity is greater than the maxSpeed...
    if (Mathf.Abs (rigidbody2D.velocity.x) > maxSpeed)
    rigidbody2D.velocity = new Vector2 (Mathf.Sign (rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);

    Seems that he actually uses the velocity method when max speed is reached. I think I'm gonna try the opposite force that you mentioned.

    Thanks again