- Home /
When specifying rigidbody
I implemented a dash mechanic and I am trying to carry the velocity the player had before dashing to after he is done dashing. I made Vector2 originalVelocity and set it equal to myrigidbody.velocity and it is working, only problem is I don't want it to carry over the y velocity and only the x. However, when I set originalVelocity equal to myrigidbody.velocity.x, I get the error "Cannot implicitly convert type 'float' to 'UnityEngine.Vector2." I only get this error when specifying x. Here is my code:
IEnumerator Dash(float dashDuration, float dashCooldown)
{
Vector2 originalVelocity = myrigidbody.velocity.x;
isDashing = true;
canDash = false;
myrigidbody.gravityScale = 0;
myrigidbody.velocity = Vector2.zero;
yield return new WaitForSeconds(dashDuration);
isDashing = false;
myrigidbody.gravityScale = normalGravity;
myrigidbody.velocity = originalVelocity;
yield return new WaitForSeconds(dashCooldown);
canDash = true;
}
Can somebody explain how I can achieve this?
Answer by CodesCove · Mar 16, 2021 at 08:49 PM
Create new Vector2 with correct x and y values and set is to velocity. Below code will carry over the x but keeps the y same as it is:
myrigidbody.velocity = new Vector2(originalVelocity.x, myrigidbody.velocity.y);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Collision triggers do not work on child gameobject 3 Answers
C# Convert Vector3[] to Vector2[] 3 Answers
[2D] Add force to all colliders within a certain radius 1 Answer