RigidBody2D velocity randomly drops to zero
I'm using a rigibody2D on a cube in Unity. I have a constant force been added to slowly increase the speed and couple of ifs to cap it out if max speed is reached. This works but every now and then before fixed update the velocity randomly goes to zero. In addition at the time when it drops to zero it goes to some random numbers for a couple of frames (for example -5.453111E-16)
void FixedUpdate()
{
float horizontalAxis = Input.GetAxis("Horizontal"); //gets horizontal axis
Debug.Log("axis: " + Vector2.right * horizontalAxis * speed);
Debug.Log("velocity.x before addforce: " + rb2d.velocity.x);
rb2d.AddForce(Vector2.right * horizontalAxis * speed); //adds x force based on input and speed property
Debug.Log("velocity.x after addforce: " + rb2d.velocity.x);
//cap out speed to max if more than max (abs)
if(rb2d.velocity.x > maxSpeed)
{
Debug.Log("velocity.x over max: " + rb2d.velocity.x);
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
Debug.Log("capped velocity.x: " + rb2d.velocity.x);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
Image of player components:
hi, i now that is an old topic but i have the same problem with my character, it suddenly began to have this behaviour where its rigibody2d drops its y speed to zero without any reason , so the cuestion is... did you solve it?? and how?? i been searching for all the internet without a clear answer
Since that was long ago I don't really remember if that was the case but I think my floor was tiled and it was stopping on the edges of the tiles.
Answer by hexagonius · Dec 17, 2015 at 11:35 PM
I think it has to do with negating the scale on direction change, which might be bad in your, but is bad in a second case. instead of scaling the transform, put the visual as child object and ROTATE it around y axis. This might solve your problem as well as not break batching.
Nope, even if I comment out that part it still drops down to zero. In addition if I release the input and then press it again still doesnt move.
Your answer
Follow this Question
Related Questions
2D : How to overwrite rigidbody velocity when "skill" input pressed ??? 0 Answers
rigidbody.velocity.x doesn't work :( 2 Answers
Trying to add force to an object... is this done correctly? 0 Answers
AddForce rigidbody 2d not moving object 2 Answers
how to: Velocity or addForce to 2D (Instantiated?) Object 1 Answer