Help with AI patrol

Help!!! I’ve made a script which sends the enemy one way, but I can’t figure out how to make him go back, and repeat!!!

Here’s the script : can you try to tell me what to do?

using UnityEngine;
using System.Collections;

public class SimpleEnemyScripy : MonoBehaviour {

	public float velocity = 1f;

	public Transform sightStart;
	public Transform sightEnd;

	public LayerMask detectWhat;

	public bool colliding;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
		rigidbody2D.velocity = new Vector2 (velocity, rigidbody2D.velocity.y);

		colliding = Physics2D.Linecast (sightStart.position, sightEnd.position, detectWhat);

		if (colliding) {

			transform.localScale = new Vector2 (transform.localScale.x * -1, transform.localScale.y);
			velocity*= -1;

				}

	}

	void OnDrawGizmos()
	{
		Gizmos.color = Color.magenta;

		Gizmos.DrawLine (sightStart.position, sightEnd.position);
	}

}

thanks!!!

I am getting same problem. link text

Hi,

I had a very similar problem.

the way I fixed it conceptually is that you have to make different objects for the guard to hit, then when the guard hits the object, THE OBJECT modifys the variable on the guard that tells it where to go. (so make the guard look at object number one, then when object one registers a hit of the guard it would set the target of the guard to target 2 and so on)

Im not putting any code here because you write in C# and I write in java but the concept is the same.

I’m not sure if this will satisfy your question, but you could just drop two empty game objects in your scene and have it switch between those.

if(close enough to one point)
{
Switch to the other one
}

and then you can just keep moving your enemy with the rigidbody.

Also, this way you can make an array of waypoints and have him loop those for example of pick random ones.

Hope it helps.