Random movement, staying in an area.

Hey I have acquired a script that allows for random movement, the problem is my object just wanders off the plane and ignores collision. Is there anyway I can set a boundary for this random movement to stop it venturing off the plane and into dead space? Thanks, here’s what I have so far:

var chicken : Transform; 
var vel : Vector3;
var Direction : float = 3;
var curTime : float = 0;
function Start()
{
  SetVel();
}
function SetVel()
{
  if (Random.value > .5) {
    vel.x = 10 * 10 * Random.value;
  }
  else {
    vel.x = -10 * 10 * Random.value;
  }
  if (Random.value > .5) {
    vel.z = 10 * 10 * Random.value;
  }
  else {
    vel.z = -10 * 10 * Random.value;
  }
}

function Update()
	{
  		if (curTime < Direction) {
  		curTime += 1 * Time.deltaTime;
  }
  
  else {
    	SetVel();
    		if (Random.value > .5) {
     		 Direction += Random.value;
}

  else {
      Direction -= Random.value;
  }
    	if (Direction < 1) {
      	Direction = 1 + Random.value;
    }
    curTime = 0;
  }
}


function FixedUpdate()
	{
  chicken.rigidbody.velocity = vel;
}

Any help is appreciated :slight_smile:

This is what I did 2 hours ago. It allows a random movement throught a surface which limits I have established manually. It sets the correct rotation of the object too, but be careful with the correction of 90º degrees I had to do and apply yours. I hope this helps to fix your code.

public float velocidadMax;
    
    public float xMax;
    public float zMax;
    public float xMin;
    public float zMin;
        
    private float x;
    private float z;
    private float tiempo;
    private float angulo;

	// Use this for initialization
	void Start () {


        x = Random.Range(-velocidadMax, velocidadMax);
        z = Random.Range(-velocidadMax, velocidadMax);
        angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
        transform.localRotation = Quaternion.Euler( 0, angulo, 0);
	}
	
	// Update is called once per frame
	void Update () {

        tiempo += Time.deltaTime;

        if (transform.localPosition.x > xMax) {
            x = Random.Range(-velocidadMax, 0.0f);
            angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
            transform.localRotation = Quaternion.Euler(0, angulo, 0);
            tiempo = 0.0f; 
        }
        if (transform.localPosition.x < xMin) {
            x = Random.Range(0.0f, velocidadMax);
            angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
            transform.localRotation = Quaternion.Euler(0, angulo, 0); 
            tiempo = 0.0f; 
        }
        if (transform.localPosition.z > zMax) {
            z = Random.Range(-velocidadMax, 0.0f);
            angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
            transform.localRotation = Quaternion.Euler(0, angulo, 0); 
            tiempo = 0.0f; 
        }
        if (transform.localPosition.z < zMin) {
            z = Random.Range(0.0f, velocidadMax);
            angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
            transform.localRotation = Quaternion.Euler(0, angulo, 0);
            tiempo = 0.0f; 
        }


        if (tiempo > 1.0f) {
            x = Random.Range(-velocidadMax, velocidadMax);
            z = Random.Range(-velocidadMax, velocidadMax);
            angulo = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
            transform.localRotation = Quaternion.Euler(0, angulo, 0);
            tiempo = 0.0f;
        }

        transform.localPosition = new Vector3(transform.localPosition.x + x, transform.localPosition.y, transform.localPosition.z + z);
	}

Check this Video For Random MoveMent :
Unity AI Random Movement ( Based on Area and Based on enemy) - YouTube