- Home /
2D Sprite drifting down, otherwise passes through walls - how do I fix these issues?
My 2D sprite is the player character in a top down game.
The sprite moves fine, but when it's not supposed to be moving- be still - it slowly drifts downwards on the default settings for Rigidbody 2D.
When I turn the Rigidbody 2D Body Type from Dynamic to Kinetic, the player character no longer drifts, but the player character now passes through the walls. It seems the collider no longer works?
How do I fix the drift issue without having the issue of the character passing through walls?
I don't think it's an issue with my movement code, but just in case here it is:
public class MoveCharacter : MonoBehaviour
{
// variable to refer to our rigidbody
Rigidbody2D rbody;
public float moveSpeed = 5f;
void Start()
{
rbody = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
//figure out which way to move
Vector2 movement;
movement.x = Input.GetAxis("Horizontal");
movement.y = Input.GetAxis("Vertical");
//figure out where my new position will be
Vector2 nextPosition = rbody.position + movement * Time.fixedDeltaTime * moveSpeed;
//tell the rigid body to move to that new position
rbody.MovePosition(nextPosition);
}
}
Your answer
Follow this Question
Related Questions
Rigidbody2d Floating Above Ground 5 Answers
Trying to get actual good arrow physics in 2D 1 Answer
how to make a 2d character three-dimensional? 1 Answer
Default Contact Offset Problems 1 Answer