Simple Enemy Pacing Script [Unity3D]

Unity3D Version: 4.3.3f1

This C# code is for a simple enemy pacing script. The GameObject that has this script attached will go back and fourth in any Axis that is desired (default is to pace in the X-Axis). The GameObject uses a Ridged Body which makes it is quick to add other mechanics to it, such as jumping for the GameObject being using:

this.rigidbody.AddForce(Vector3.up * upForce);

or

this.rigidbody.AddForce(Vector3.down * downForce);


CODE


 

[code language=”java”]

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Rigidbody))]

public class EnemyPace : MonoBehaviour {

   public Vector3 pointA;
   public Vector3 pointB;
   public Vector3 speed = new Vector3(3, 0, 0);

   // Distance the object will move horizontally.x, vertically.y, and z-ically.z
  public Vector3 moveDistance = new Vector3(7, 0, 0);

   // Enemy pace – default : always on
  public int paceDirection = 1; // 1 = move right, -1 = move left
  public Quaternion lookLeft = Quaternion.Euler(0, 0, 0);
  public Quaternion lookRight = Quaternion.Euler(0, 180, 0);

    void Start ()
    {
        pointA = this.rigidbody.position;
       pointB = pointA + moveDistance;
    }

    void FixedUpdate()
   {
      // Decides pace direction, 1 = Right, -1 = Left
      if (rigidbody.position.x >= pointB.x && paceDirection == 1)
      {
          paceDirection = -paceDirection;
          transform.rotation = lookRight;
      }
      else if (rigidbody.position.x < pointA.x && paceDirection == -1)
      {
          paceDirection = -paceDirection;
          transform.rotation = lookLeft;
      }

       // Moves Object with Ridgebody left and right
      this.rigidbody.MovePosition(rigidbody.position + (paceDirection * speed) * Time.deltaTime);

    }
}

[/code]