title: Unity 3D 2D Enemy Patrol
body: <p>This code is for enemy AI in unity 2D game. Right now the enemy is standing still but if i change both IF,s &lt; or > then enemy is moving right or left direction and nothing more. </p>
  
  <p>I am trying to make enemy AI patrol system. Got everything else done (enemy triggering system and enemy chasing player), but only this patrol part is not working.</p>
  
  <p>I got one working patrol code in javascript but i need it in C# so everything would be inside one c# script file.</p>
  
  <pre><code>using UnityEngine;
  using System.Collections;
  
  public class VastaneAI : MonoBehaviour {
  
      protected Vector3 velocity;
      public Transform _transform;
      public float distance = 1f;
      public float speed = 1f;
      Vector3 _originalPosition;
  
      public void Start () {
          _originalPosition = gameObject.transform.position;
          _transform = GetComponent&lt;Transform&gt;();
          velocity = new Vector3(speed,0,0);
          _transform.Translate ( velocity.x*Time.deltaTime,0,0);
      }
  
      public void Update(){
          if (transform.position.x &lt; _originalPosition.x-distance) {
              Debug.Log(_originalPosition.x-distance+"Left side");
              _transform.Translate ( (1*velocity.x)*Time.deltaTime,0,0);
          }   
          if (transform.position.x &gt; _originalPosition.x+distance) {
              Debug.Log(_originalPosition.x+distance+"right side");
              _transform.Translate ( -velocity.x*Time.deltaTime,0,0);
              //velocity = velocity*-1;
          }   
      }
  }
  </code></pre>