Enemies can walk through walls

Following is the code I use on enemies.

void FixedUpdate () {
		transform.LookAt (player.transform);
		if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out shot)) {
			targetDistance = shot.distance;
			if (targetDistance < allowedRange) {
				enemySpeed = 0.6f;
				if (attackTrigger == 0) {
					enemy.GetComponent<Animation> ().Play ("Walking");
					transform.position = Vector3.MoveTowards (transform.position, player.transform.position, enemySpeed * Time.deltaTime);
					//GetComponent<Rigidbody>().MovePosition(transform.position + transform.forward * Time.deltaTime);
				}
			} else {
				enemySpeed = 0;
				enemy.GetComponent<Animation> ().Play ("Idle");
			}
		}

		if (attackTrigger == 1) {
			if (isAttacking == 0) {
				StartCoroutine(EnemyDamage());
			}
			enemySpeed = 0;
			enemy.GetComponent<Animation>().Play("Attacking");
		}
	}

@Arcane94 Basically I think the issue is that you are using transform for moving your enemy. Collision are not effected on transform changes. Transform simple teleports from one point to second point. You should add a rigidbody if not added and use Addforce for this. Only then you will see the effects of collisions.

Why don’t you just use a NavMeshAgent? NavMeshAgents deal with obstacles and have many properties to play around with to fit navigational needs.

I would make sure there’s a collider on the enemy and the walls. The collider on the wall should be just a little wider than the wall surface.
Yes, NavMeshAgent is useful, but if you won’t use that, then try a raycasting method. The raycast should aim in the same direction the enemy is looking/facing. If the player is close by and the raycast picks up a surface not relative to the player, you can have the enemy’s transform move another direction until the raycast line picks up the player, then return the transform movement like normal.
Just an idea.

I would suggest you should use the NavMeshAgent for the Enemy, it will automatically detects various collisions and paths for the Enemy to move.

Note:Make sure You bake your Scene Environment else NavMeshAgent(Enemy) will not detect the path to move and stays still.

Never use transform or forces! But instead you can use the velocity on the rigidbody :slight_smile:

Exemple :

this.GetComponent<Rigidbody2D>().velocity = Vector2.ClampMagnitude(this.GetComponent<Rigidbody2D>().velocity + new Vector2(Input.GetAxis("Horizontal") * 10, Input.GetAxis("Vertical") * 10), 20);

Note that I am clamping the magnitude to avoid going too fast and multiplying the axis to have more reactivity. You have to add something to the velocity, not assign it, else the gravity won’t work anymore.