Character is chopping through the terrain

So I am making a simple platformer. I used a Tile map system to build the terrain. Its jus a single color.

To add collision detection, I gave my character a rigidBody2D and a box collider and I gave the grid a 2D tile Map collider. Now whenever I am colliding a wall, my character crops into the wall and starts behaving weirdly.

I tried changing collision detection to continuous but it made very minor difference

PLEASE HELP ME FIX THIS!!

How are you moving the character? When using physics, the way you move the character can sometimes cause issues because we are moving characters into areas that it shouldn’t be allowed to go, and then the physics kicks in and tries to undo that, and then weird behaviour happens.

Just standard input method

public int moveSpeed;

public GameObject blok;
public Rigidbody2D rb;

private void Update()
{
    float xAxis = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
    transform.Translate( new Vector2(xAxis, 0));
}

}

Yea, that’s the problem. transform.Translate does not concern itself with physics. So, with translate the character can move into the terrain, and then the physics detects this and pushes you out of the terrain again.

The TileVania section of the Unity 2D course (among other) covers movement that uses the rigidbody to move

You set the velocity of the character instead of translating it

private Vector2 moveInput;

private void Update()
{
    moveInput = new Vector2(Input.GetAxis("Horizontal"), 0f) * moveSpeed;
}

private void FixedUpdate()
{
    rb.velocity = moveInput;
}

There’s a lot more to it, but this now moves and takes collisions into account

Any specific reason to use FixedUpdate btw?

Btw now the physics are acting weird

public int moveSpeed;
Vector2 xAxis;

public GameObject blok;
public Rigidbody2D rb;

private void Update()
{
    xAxis = new Vector2(Input.GetAxis("Horizontal"), 0f) * moveSpeed;
    rb.velocity = xAxis;
}

Now the Object is not falling to the ground as intended :slightly_smiling_face:

Physics happen on a fixed interval. Physics stuff should be done in FixedUpdate()

This is because you are controlling the velocity and not allowing the physics to do it. If you want gravity to come into play, you need to keep the y-velocity of the rigidbody

public int moveSpeed;
float xAxis; // don't need a vector

public GameObject blok;
public Rigidbody2D rb;

private void Update()
{
    xAxis = Input.GetAxis("Horizontal");
}
private void FixedUpdate()
{
    Vector2 movement = new Vector2(xAxis * moveSpeed, rb.velocity.y); // keep the y-velocity
    rb.velocity = movement;
}

With this, the physics engine’s gravity will still be applied

I recommend getting the course I mentioned (if you don’t have it). It covers all this movement and more

Yeah I have that course on my wishlist. I would take that ASAP. Anyway thank you for helping me!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms