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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Inconsistent results when applying velocity towards an object to simulate a player air dash.

Discussion in 'Physics' started by Fredex, Oct 16, 2015.

  1. Fredex

    Fredex

    Joined:
    Oct 16, 2015
    Posts:
    4
    Hello!
    I am currently trying to implement an 'air dash' mechanic for my player. The direction of the dash is based on the position of an 'armEnd' object which will rotate around the player based on the position of a user's touch on screen. This armEnd is a child object of a cube gameObject which follows the player; armEnd's distance or radius from the player should stay the same, with only the position or direction around the player changing. I should note that my game is in 2D space.

    The following code is from my airDash() function which is called from Update() only once when specific conditions are met, namely the player is in the air, has tapped and then released that tap. These are part of a script that is attached to my Player gameObject.

    Code (CSharp):
    1.  
    2.         velocityV3 = (armEnd.transform.position - transform.position);
    3.         velocityV3.Normalize ();
    4.         //float magnitude = Mathf.Sqrt (Mathf.Pow (velocityV3.x, 2) + Mathf.Pow (velocityV3.y, 2) + Mathf.Pow (velocityV3.z, 2));
    5.         rb2d.velocity = velocityV3 * airDashForce * 80 * Time.unscaledDeltaTime;
    6.         Debug.Log ("Player Pos : " + transform.position + " || Arm End : " + armEnd.transform.position + " || rb2d velocity : " + rb2d.velocity);
    7.  
    ^Where airDashForce is just a public float used for testing, and 80 felt like a good multiplier to use while testing to not have my airDashForce be a super big number.

    The ideal results i'm looking for in this situation is to produce an airdash of the same magnitude each time this is called, but have the direction of the velocity directed towards the armEnd object. My current results with this have been very inconsistent:
    inconsistent jump.png
    Here you can see that very similar player and armEnd positions resulted in very different velocities. Some dashes will look fine, while others will way over shoot the mark or fall way short.

    Anyway I was hoping that someone could suggest a solution or point out something that I am obviously doing wrong here, any input helps. Thanks

    Notes:
    • I have tried to achieve the air dash by lerping the player to the armEnd position which looked pretty nice, and achieved mostly what I was looking for visually BUT it was causing a number of collision detection problems with a number of platforms / the ground plane in my scene.
    • I have tried using rigidbody2d.addForce(blahblah) here instead of rigidbody2d.velocity = (blahblah) but that seemed even less consistent.
    • I am using Time.unscaledDeltaTime here as I have a slowdown effect before the airDash, this problem was around before I decided to add the slowdown effect, and using time.deltaTime with no slowdown/time scaling present in the scene produced similar results.
     
    Last edited: Oct 16, 2015
  2. Fredex

    Fredex

    Joined:
    Oct 16, 2015
    Posts:
    4
    The code mentioned above is from a Dash Control script attached to my player, and I would like to add info on the other components attached to my character to help with any available input.
    playerInfo.png
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    not sure if I understood your problem completely, so it seems you are at point A and tap do whatever at point B, and then change the velocity so that you are going into that direction... and you observe that velocity is different, yet the A and B are same or nearly same?

    Could it be just the fact that you are multiplying your value with deltaTime (or unscaledDeltaTime) ?

    Your game does not run on constant speed for every frame it takes different time to render it, thus different delta. And for velocity, it's constant value that get's used in FixedUpdate AFAIK.

    IMO your velocity should be just set, let physics system use it as it is. It shouldn't be multiplied by changing deltaValue You don't want to set different velocity for your rigidbody, if your last frame happened to take long time to update and render... that way depending on frame deltaTime, you will have slightly different velocity everytime you tap.

    I'm just guessing so better test this unless someone who knows for sure can comment on this.
     
    Fredex likes this.
  4. Fredex

    Fredex

    Joined:
    Oct 16, 2015
    Posts:
    4
    Heya,

    Thanks for the response! I've been meaning to comment my solution to this, and it was indeed what you mentioned, I should NOT have been multiplying my Velocity by time... this was left over from me applying force to the player before deciding to just set the velocity instead. A pretty silly mistake. Appreciate the input.
    Cheers.