- Home /
[C#, 2D] How do I apply force to a player using vector 3 velocity to move
Forgive me if I can't describe the issue well enough, but I'm trying to use AddForce to 'boost' my player forward, but to use addForce i have to suspend my rigidbody's normal movement code, since they boh use X velocity. The issue is that when my player is done 'dashing' they will stop mid air, and not carry the velocity forward.
My regular movement code is
void FixedUpdate()
{
if (isDashing == false)
{
if (isSliding == false)
{
if (touchWall == false)
{
playerBody.gravityScale = 1.5f;
Vector3 moveVelocity = new Vector3(Input.GetAxis("moveX") * (playerSpeed * 50.0f) * Time.fixedDeltaTime, playerBody.velocity.y, 0.0f);
playerBody.velocity = moveVelocity;
}
if (touchWall == true)
{
playerBody.gravityScale = 0f;
Vector3 moveVelocity = new Vector3(Input.GetAxis("moveX") * (playerSpeed * 50.0f) * Time.fixedDeltaTime, Input.GetAxis("moveY") * (playerSpeed * 50.0f) * Time.fixedDeltaTime, 0.0f);
playerBody.velocity = moveVelocity;
}
}
}
}
My Dashing code is
void Dash()
{
if (right == false)
{
turnt = -1;
} else if (right == true)
{
turnt = 1;
}
Vector3 moveVelocity = new Vector3((slideSpeed * 50.0f * turnt) * Time.fixedDeltaTime, playerBody.velocity.y, 0.0f);
playerBody.velocity = moveVelocity;
StartCoroutine("DashW8");
}
IEnumerator DashW8()
{
yield return new WaitForSeconds(slideW8);
isDashing = false;
}
Answer by ray2yar · Dec 27, 2018 at 04:52 PM
Maybe try using a "maxVelocity" variable and temporarily increase it while dashing. This way you can make your "moveVelocity" higher for a limited time and then decrease it after the duration with out mixing in any Addforce.
Your answer
![](https://koobas.hobune.stream/wayback/20220612184111im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
move 2d character affected by physics with velocity and/or add force 2 Answers
Dashing with rigidbody2D not working right 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
How to go about having two seperate Game Objects follow eachother? 1 Answer
Stuttering/jerky movement of kinematic rigidbody 2d moving by velocity in 2D game on iOS 0 Answers