How to smooth movement in Unity 4.3 2D with AddForce?

10,967

Ok i could solved the problem. My problem was that i only did this AddForce only once. The AddForce code was only executed in a single frame. I added a time which i count down to define how long the dash movement gonna be!

Share:
10,967
Verdemis
Author by

Verdemis

Updated on June 04, 2022

Comments

  • Verdemis
    Verdemis almost 2 years

    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?

    // Dash
    rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
    // Jump
    rigidbody2D.AddForce (new Vector2(0, jumpForce));
    

    Best, Verdemis :)

    EDIT: I can not access my project at the moment, but i will try to show you what i have done.

    Note: dashSpeed is a float value, at the moment something like 3500 and direction contains 1 or -1, depending on the direction the player is looking. The Dash code is part of the Update method.

    // Dash
    if(Input.GetKeyDown(dashKey))
    rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
    
  • Verdemis
    Verdemis about 10 years
    I added some information above. Thank you for your answer.
  • Esa
    Esa about 10 years
    @Verdemis I updated the answer. Since the vector is normalized the use of Update-loop instead of FixedUpdate is the only fix I can think of now.
  • Verdemis
    Verdemis about 10 years
    Ok thank you. I'm not sure if i allready did move it to the fixedUpdate. But i will check it later.