Player Going through colliders

Hi everyone, Im making a 2D game, but my Player goes through colliders. I use touchscreen buttons to control my player. The code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour {
//variables
public float moveSpeed = 300;
public GameObject character;

private Rigidbody2D characterBody;
private float ScreenWidth;

// Use this for initialization
void Start () {
	ScreenWidth = Screen.width;
	characterBody = character.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {
	int i = 0;
	//loop over every touch found
	while (i < Input.touchCount) {
		if (Input.GetTouch (i).position.x > ScreenWidth / 2) {
			//move right
			RunCharacter (1.0f);
		}
		if (Input.GetTouch (i).position.x < ScreenWidth / 2) {
			//move left
			RunCharacter (-1.0f);
		}
		++i;
	}
}
void FixedUpdate(){
	#if UNITY_EDITOR
	RunCharacter(Input.GetAxis("Horizontal"));
	#endif
}

private void RunCharacter(float horizontalInput){
	//move player
	characterBody.transform.Translate(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));

}

}

Whenever you’re moving a Rigidbody around, you should almost always use MovePosition instead of Translate.

Translate is for moving Transforms, whereas MovePosition is specifically for Rigidbodies.

Here’s how you could re-write your RunCharacter method:

private void RunCharacter(float horizontalInput)
{
    Vector2 movementDirection = new Vector2(horizontalInput, 0);
    rb.MovePosition(rb.position + movementDirection * moveSpeed * Time.deltaTime);
}

If you haven’t already, I also recommend going to your player’s Rigidbody and setting Collision Detection to “Continuous.” This will help make the collision detection more accurate. You may also want to consider setting Interpolate to “Interpolate” to make your player’s movement less jittery, but that’s entirely up to you.