title: 3d Unity C# enemy walks through walls
body: <p>Hello wizards of the internet,</p>
  
  <p>I'm making a 3D first person RPG where you can attack enemies which are located around the map. My current script is working fine, however there is one problem: the enemies are walking through walls whenever they are focused on the player.</p>
  
  <p>I've already tried a few things to fix this, but unfortunately there was no result. Does anyone know a solution to this problem?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>goblinAttack.cs:</strong></p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class goblinAttack : MonoBehaviour {
  
      public Transform player;
      static Animator anim;
  
      // Use this for initialization
      void Start () 
      {
          anim = GetComponent&lt;Animator&gt;();
      }
  
      // Update is called once per frame
      void Update () 
      {
          Vector3 direction = player.position - this.transform.position;
          float angle = Vector3.Angle(direction,this.transform.forward);
          if(Vector3.Distance(player.position, this.transform.position) &lt; 10 &amp;&amp; angle &lt; 30)
          {
  
              direction.y = 0;
  
              this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                          Quaternion.LookRotation(direction), 0.1f);
  
              anim.SetBool("isIdle",false);
              if(direction.magnitude &gt; 5)
              {
                  this.transform.Translate(0,0,0.10f);
                  anim.SetBool("isRunning",true);
                  anim.SetBool("isAttacking",false);
                  anim.SetBool("isCrying",true);
              }
              else
              {
                  anim.SetBool("isAttacking",true);
                  anim.SetBool("isRunning",false);
                  anim.SetBool("isCrying",false);
              }
  
          }
          else 
          {
              anim.SetBool("isIdle", true);
              anim.SetBool("isRunning", false);
              anim.SetBool("isAttacking", false);
              anim.SetBool("isCrying",false);
  
          }
  
      }
  }
  </code></pre>
  
  <p><strong>NPC settings:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/DnEfQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DnEfQ.png" alt="settings"></a></p>