Practicing Scipting

So im practicing scripting and am trying to make an simple AI that moves around inside boundaries. I started with:

transform.Translate(Vector3(0,0,2) * Time.deltaTime);

So that it would move, but im having trouble making boundaries:

var vec : Vector3 = Vector3(5,0,5);

function Update () {

    if (rigidbody.position.z >= vec.z) {
         transform.Translate(Vector3(2,0,0) * Time.deltaTime);
    }

    else if (rigidbody.position.x >= vec.x) 
    {
         transform.Translate(Vector3(0,0,2) * Time.deltaTime);
    }

    else {
         transform.Translate(Vector3(0,0,2) * Time.deltaTime);
    }
}

Can someone fix it so it has boundaries and when it hits the boundaries it goes in a random direction?

Practice scripting isn't a question.

Your question isn't a real question, but the answer is: Yes. Someone can fix it so it has boundaries and when it hits the boundaries it goes in a random direction. But I'm pretty sure you could have figured that out without asking

If you want to know how to constrain something's values or how to go in a random direction, you should look into and if need be, ask those questions. UnityAnswers is for questions - specific ones. If you have a request or want to go into discussion about something, put it in the forums. Please read the FAQ.

First, understand what the code you wrote does:

//This, I assume is what you meant by a boundary?
var vec : Vector3 = Vector3(5,0,5);

function Update () {
    //if the position is past the boundary z
    //move 2 on the x axis every second
    if (rigidbody.position.z >= vec.z)
         transform.Translate(Vector3(2,0,0) * Time.deltaTime);

    //if the position is past the boundary x
    //move 2 on the z axis every second
    else if (rigidbody.position.x >= vec.x) 
         transform.Translate(Vector3(0,0,2) * Time.deltaTime);

    //in any other situation, move 2 on the z axis
    else
         transform.Translate(Vector3(0,0,2) * Time.deltaTime);
}

What does this do?

  1. If we don't start past the boundary z or x, move ~5 on the z axis in ~2.5 seconds.
  2. If we don't start past the boundary z, move ~5 on the x axis in ~2.5 seconds.
  3. Move about 2 on the z axis every second.

Which will result in:

  • If you start past the x boundary, it will continue on the z axis forever.
  • If you start past the z boundary, it will move on the x and then the z.
  • otherwise it will move on z, then x, then z.

It doesn't even look like you've tried at all to accomplish what you've asked for. Try a little. Search a little. Think about it a bit. Research. If all of that fails, then ask for help. If you don't care enough to even try, why should anyone else?

Some of this may suit your purpose:

There's a lot of stuff on pathfinding and AI if you want something more complicated. See npcs-random-movement.

There is a class called Random which has the functions Random.value and Random.Range and Random.onUnitSphere. If you want something random, try working with those. Alternatively a good way is to generate some noise as a texture and then sample the texture clamping time as an index somehow if you want more control over the values and randomness.

If you want a random movement, you could easily do something like use a vector with randomly generated values in the range -1 and 1. You could use a random value to index an array of movements or something like that. Using noise sampling gives you a bit more control over how random it is. To smooth the motion, you could try lerping between values that are generated less frequently or you could try adding one random vector to another to get an additive randomness.

If you want to ensure something stays within a boundary, the answer is simple : stay within the boundary.

You can simply move towards a number of randomly generated points that are within the boundary and then you will never leave the boundary, lerping over time.

var destination : Vector3;

function Start() {
    destination = transform.position;
}

function Update() {
    if (destination == transform.position) {
        //You could just as easily generate a square boundary
        //destination = new Vector3(Random.Range(minX, maxX),
        //                          Random.Range(minY, maxY),
        //                          Random.Range(minZ, maxZ));
        //Or pick from an array of defined waypoints.
        var point : Vector2 = Random.insideUnitCircle * 5; //Boundary circle size 5
        destination = new Vector3(point.x, 0, point.y);
    }
    transform.position = Vector3.Lerp(transform.position, destination, Time.deltaTime);
}

If you don't know your boundary, the simple answer for something like this is to check something like if(exitedBoundary()) returnToBoundary(); Define what exiting and returning to the boundary are and you're done. In the simple case, if you hit or cross a boundary, move in the opposite direction of that which you did to cross it.

If you want to randomly move back once you hit the boundary, then calculate the vector of the boundary that you crossed/hit, generate a random angle that is less than 180 degrees and rotate that vector by that angle in the direction inside of the boundary. If concerned about a 3D boundary, generate two rotation, one less than 180 degrees; At your contact point, the 3D boundary will have a normal; Choosing a vector along the plane according to your unbound rotation, you will then have a vector which you can rotate the the inside of the boundary by the second rotation.

I'm not really sure how you are going to define the boundaries without using primitive geometry and if you did, I'm not sure how you'd know you crossed it. I mean, you could maintain an edge and/or point list indicating boundaries and you'd have to define what is inside and what is outside - in some cases this is done by ordering the list in a specific way - but then every frame (you could probably do it less often if you aren't moving very fast), you'd need to check against every edge to see if you crossed a boundary. It could be kind of expensive. You might consider creating a representative 3D volume with a mesh collider set as a trigger and using the OnTriggerExit event or if you feel the need, raycast against it yourself on update to see if you've exited.