Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to smooth movement with AddForce?

Discussion in '2D' started by Verdemis, Mar 16, 2014.

  1. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Hey Guys und Girls,

    at the moment i'm working on a 2D plattformer and the work is going very well. But there is one Problem i can't get rid of.

    The player can use a dash, which should move the player very fast in the direction he is looking. My problem is, that the player game object instant appears at the target location... like a teleport.

    I'm using the AddForce Function to move the game object. On jumping i'm using AddForce as well and there it works really nice, i get a smooth jump movement.

    The only different between dash and jump is that on jump i apply force to the y axis and on dash to the x axis. Changing the amount of force doesn't affect the movement only the distance.

    Does anyone have an idea what i'm doing wrong?

    Code (csharp):
    1. // Dash
    2. rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
    3. // Jump
    4. rigidbody2D.AddForce (new Vector2(0, jumpForce));
    5.  

    Best,
    Verdemis :)
     
  2. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Hey Verdemis. I'm having a similar problem; however you're doing better than I am. You say the object can jump smoothly, right? If so, I would appreciate it if you would tell me how you did it because I can't get mine to jump. It just teleports, which I don't want. I'm new to coding as well, with both c# and js, but I'm more fluent with js, but still barely know it. I apologize for not being able to help you, but maybe I could if you tell me how you jump.

    ~ Chris
     
  3. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Nevermind, I got it. I'm not sure if you would still like an answer to your question or not, considering you asked this question over 5 months ago, but if you want one I can answer.

    -- Chris
     
  4. Snipe76

    Snipe76

    Joined:
    May 23, 2017
    Posts:
    12
    Hey! I am having a similar problem, after 4 years I hope you still remember the answer.
    Can you please tell me what was the solution?

    same as you if you know the answer..
     
  5. HaddicusGames

    HaddicusGames

    Joined:
    May 28, 2014
    Posts:
    28
    Add force can indeed make it seem as if the player is dashing, or moving in a specific way, using add-force is often not the best option for this, especially if you want to show an effect behind the movement (which is often the case). Moving the player through disabling the rigidbody, handling the movement through code, then re-enabling the rigedbody at the end of the movement, is generally a really good way to handle the movement, in these cases. It gives you full control over how the visual takes place, for the character movement, and you can apply different sprites through the animation.
     
  6. Verdemis

    Verdemis

    Joined:
    Apr 27, 2013
    Posts:
    92
    Could you show me some code? Maybe I can help you :)
     
  7. Snipe76

    Snipe76

    Joined:
    May 23, 2017
    Posts:
    12
    Last edited: Apr 8, 2018