How to stop coliders from overlaping? (2D)

Hello. I’m a total noob and I’m trying to make a simple 2d platformer but my colliders seem overlap when “player” runs into the wall (everything is fine with floor). Player is rigidbody2D (with a collider2D) wall and floor are just colliders2D. I’m using Vector3 for movement. In fact here is code of “player”
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {
	public float speed = 15.0f;
	public float padding = 0.5f;


	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			transform.position += Vector3.left * speed * Time.deltaTime;
		} else if (Input.GetKey(KeyCode.RightArrow))
		{
			transform.position += Vector3.right * speed * Time.deltaTime;
		}
	}
}

I tried switching to Vector2 but I get all sorts of errors (the course I’m taking right now never used Vector2 so I have no idea what I’m doing wrong).

If you want to have 2D collision, you should never change directly the transform.position, because you prevent the 2D physic engine to do his job and check for collision.

So what you want to do is use RigidBody2d.MovePosition for instance (Unity - Scripting API: Rigidbody2D.MovePosition).

something like that :

public class PlayerController : MonoBehaviour {
	public float speed = 15.0f;
	public float padding = 0.5f;

	Vector2 velocity;
	Rigidbody2D rb2D;

	// Use this for initialization
	void Start () {
		rb2D = gameObject.GetComponent<Rigidbody2D>();
		velocity = Vector2.zero;
	}

	// Update is called once per frame
	void Update () {
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			velocity = Vector2.left;
		} else if (Input.GetKey(KeyCode.RightArrow))
		{
			velocity = Vector2.right;
		}
	}
		
	void FixedUpdate() 
	{
		rb2D.MovePosition(rb2D.position + velocity * speed * Time.fixedDeltaTime);
	}
}

Hi,
you can use rigidbody.velocity rather than transform if you wish to player stop moving when he / she faced to wall or any thing that has collider attached to it. you can do somethings like this :

pubic class Player : MonoBehaviour
{
	public float speed = 1;
	
	private RigidBody2D body2D;

	privarte void Start()
	{
		body2D = GetComponent<Rigidbody2D>();
	}

	private void FixedUpdate()
	{
		if(Input.GetButton(KeyCode.LeftArrow))
		{
			body2D.velocity = new Vector2(-1,body2D.velocity.y) * speed;
		}
		else if(Input.GetButton(KeyCode.RightArrow))
		{
			body2D.velocity = new Vector2(1, body2D.velocity.y) * speed;
		}
	}
}

You can use transform too, but you must check player’s left and right with raycast to knowing that is there any collider or not, and it cost more that using rigidbody itself.
Just remember that you must use FixedUpdate when you want to manipulate rigidbody component.

I believe its because you are using ‘transform.position’ to move your rigidbody.

You should be using ‘Rigidbody.MovePosition’

You will need to refer to the rigidbody first, so add these:

public Rigidbody rb;

void Start() 
{
     rb = GetComponent<Rigidbody>();
}

Then for moving right try:

rb.MovePosition(Vector3.right * speed * Time.deltaTime);

Same goes for moving left, just change to ‘Vector3.left’.

Use layer Collision Matrix if you want to ignore collision to others. It is located in Edit - Project Settings - Physics. And check or uncheck it.