- Home /
Rigidbody2D x velocity not moving unless placed in FixedUpdate
Hello! I am having difficulties trying to get an animation event to move my character's Rigidbody2D on the X-axis. If I flip the event to happen on the Y axis, it works just fine. I have tried setting the velocity manually and AddForce. Both work on the Y-axis, but not on the X-axis.
It seems though, that if I place my Rigidbody2D code in the FixedUpdate method, then it runs and updates my velocity as expected. What could be causing this behavior?
I have attached a picture of the RB2D settings. I have already ensured that the script trying to modify the velocity is on the same GameObject. Anything else that is trying to access the RB2D is also on the parent GameObject.
Answer by logicandchaos · Jan 04, 2020 at 04:39 AM
Could we see your code as well?
I am unable to see my previous comment, for some reason. Are you able to see what I posted?
Ah! Apologies, I should have included that. $$anonymous$$y PlayerController has this method being called in the FixedUpdate.
private void Apply$$anonymous$$ovement()
{
if (!isGrounded && !isWallSliding && rewired$$anonymous$$ovementInputDirection == 0)
{
rb.velocity = new Vector2(rb.velocity.x * airDrag$$anonymous$$ultiplier, rb.velocity.y);
}
else if(can$$anonymous$$ove && isRunning)
{
rb.velocity = new Vector2(runSpeed * rewired$$anonymous$$ovementInputDirection, rb.velocity.y);
}
else if(can$$anonymous$$ove)
{
rb.velocity = new Vector2(walkSpeed * rewired$$anonymous$$ovementInputDirection, rb.velocity.y);
}
if (isWallSliding)
{
if (rb.velocity.y < -wallSlideSpeed)
{
rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
}
}
}
These next two methods are the ones I am calling as an animation event.
public void AbilityPushForward(float forwardForce)
{
rb.velocity = new Vector2(forwardForce, rb.velocity.y);
}
public void AbilityJump(float jumpForce)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
edited because I did not realize my answer had to be viewed by moderators... Apologies!
Your answer
Follow this Question
Related Questions
Rigidbody2D AddForce only working with large numbers 2 Answers
Standing on player to follow his movement,Follow movement when standing on moving platform/player 1 Answer
Dashing with rigidbody2D not working right 2 Answers
Unity 2D addforce problem 1 Answer
Adding force to rigidbody2d to slide 1 Answer