- Home /
How can I make my player air dash using velocity toward a target more consistent?
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.
velocityV3 = (armEnd.transform.position - transform.position);
velocityV3.Normalize ();
//float magnitude = Mathf.Sqrt (Mathf.Pow (velocityV3.x, 2) + Mathf.Pow (velocityV3.y, 2) + Mathf.Pow (velocityV3.z, 2));
rb2d.velocity = velocityV3 * airDashForce * 80 * Time.unscaledDeltaTime;
Debug.Log ("Player Pos : " + transform.position + " || Arm End : " + armEnd.transform.position + " || rb2d velocity : " + rb2d.velocity);
^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:
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612041949im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Make a player jump tile 1 Answer
Reproducing the "spin" effect of Flick Kick Football 1 Answer