- Home /
Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :(
Hi there,
This is my first time posting so thankyou so much to anyone that can help. This has happened in the last two games i've tried to make and its driving me nuts!
I've got a RigidBody2D being controlled by Input.GetAxisRaw and a simple move method.
void Update () {
Move(Input.GetAxisRaw("Horizontal"));
if (Input.GetButtonDown("Jump"))
Jump();
}
public void Move(float horizontalInput)
{
if (horizontalInput == 1)
{
santaAnimator.SetBool("running", true);
santaSprite.flipX = false;
santaCollider.offset = new Vector2(-0.84f, santaCollider.offset.y);
}
else if (horizontalInput == -1)
{
santaAnimator.SetBool("running", true);
santaSprite.flipX = true;
santaCollider.offset = new Vector2(0.84f, santaCollider.offset.y);
}
else
{
santaAnimator.SetBool("running", false);
}
Vector2 moveVel = santa.velocity;
moveVel.x = horizontalInput * speed;
santa.velocity = moveVel;
}
It flips the sprite and also offsets the boxcollider to make sure its in line with the sprite.
However, at seemingly random points whilst running the game, the Rigidbody (santa) will just stop moving horizontally, even if the key is is held down. The 'running' animation continues, supposedly meaning that the Move method is still working and I can jump 'over' this seemingly invisible barrier and walk back through it the other way, but itll most likely happen again in a different point some time later. The only other thing the Rigidbody is colliding with is a 'ground' prefab that has a simple BoxCollider2D on it, and thats literally it.
My repo is here: https://github.com/Dagless101/snow
I'm more than happy to upload the game as a whole somewhere if you'd like to take a hollistic view. It isnt much at the moment.
Thankyou to anyone who has a look, this is driving me mad and I'm out of ideas as to what is causing it.
Answer by tanoshimi · Nov 11, 2016 at 02:15 PM
It's quite easy to get "corners" of colliders stuck on each other - you mention that this is a box collider rubbing alongside another box collider - does your invisible wall happen at the edge of a collider? The normal solution is not to use a box collider on your player but use a circle collider instead (which is also slightly cheaper)
You are a hero!! Im using two overlapping circlecolliders now and it seems to have resolved the problem. Thankyou thankyou!
What a strange thing...