Buggy Wall Collison in 2D platformer

I’m having issues with walls in my 2D platformer.

When the player walks into one, they go through teleport back and go through again.
I want to find a way to fix this.

here’s the basic movement code:

``````void Update ()
{
direction = Mathf.Round(Input.GetAxisRaw("Horizontal"));
//checks what direction the player is holding, left, right or neutral

//HORIZONTAL MOVEMENT
// Smooth movement for going left and right
if (direction == 1) // Right
{
if (Mathf.Abs(Speed) > MaxSpeed)
{
Speed = MaxSpeed; // Set Speed to MaxSpeed directly
}
else
{
Speed = Speed + Acceleration * Time.deltaTime;
}
}
else if (direction == -1) // Left
{
if (Mathf.Abs(Speed) > MaxSpeed)
{
Speed = -MaxSpeed; // Set Speed to -MaxSpeed directly
}
else
{
Speed = Speed - Acceleration * Time.deltaTime;
}
}
else
{
if (Speed > 0) // Right
{
Speed = Mathf.Max(Speed - Deceleration * Time.deltaTime, 0); // Make sure Speed doesn't go negative
}
else if (Speed < 0) // Left
{
Speed = Mathf.Min(Speed + Deceleration * Time.deltaTime, 0); // Make sure Speed doesn't go positive
}
}
transform.position = transform.position + new Vector3(Speed * Time.deltaTime, 0, 0);
}
``````

I figured it out, it isn’t the script, its in project settings in physics 2D, make sure simulation mode is set to `Update` instead of `FixedUpdate`

It fixes it and makes the player look slightly better when moving

The problem here is not the `Simulation Mode` but the fact that you were moving a `Collider`/`RigidBody` by changing it’s `transform`.

What you should have done instead is to move this object by modifying the `RigidBody` properties instead - `rb.velocity` preferably or `rb.position` if you must.

Walking into a wall collider while changing `transform.position` results in your `Rigidbody` being forcefully “teleported” partially inside this wall. Physics engine reacts naturally to this situation by correcting this situation in one way it can - be moving this `RigidBody` out of the wall EVERY FRAME. And this is “Buggy” indeed.