Random Teleport Location

I’m extremely new to scripting and need help! I’m using the standard

var destination : Transform;

function OnTriggerEnter(other : Collider) {
if (other.tag == “EvilFace”) {
other.transform.position = destination.position;
audio.Play();
}

}

“Teleport Code” (with audio!)but I have been unable to figure out how to code a random location into the teleport (or a random audio, but that’s another problem for another question). I’ve tried a random target variable but that didn’t seem to do the job. I know this is probably an extremely easy question, but I have problems wrapping my brain around scripting. If anybody can help me, and maybe explain the how and why of the random scripting? I know I’m going to be doing a lot of random in the future. Thanks!

Right now you are teleporting to wherever you placed the destination transform. If thats null, itll probly teleport you to Vector3.zero (Vector3(0,0,0)). To get a completely random position you can use Random.Range() to put in a min and max for the range.

destination.transform.position=Vector3(Random.Range(0,300),
                                       Random.Range(0,300),Random.Range(0,300));

this will place destination at some random X,Y,&Z position between 0 and 300 (Vector3(50,279,133) as an example). Very doubtful this will be what you want, but it should help with the whole “Random-ness” situation.

You should just make that the actual destination a Vector3 and randomize it in the trigger function.

var destination:Vector3;

function OnTriggerEnter(other : Collider) 
{ 
    if(other.tag == "EvilFace") 
    {
        destination=Vector3(Random.Range(0,300),
                                       Random.Range(0,300),Random.Range(0,300));
        other.transform.position = destination; 
        audio.Play(); 
    }
}

You need a math function to generate a random number for each vector position.

int num = random.Next(1000); //whatever number you pass is the max

destination.position.x=num;

num = random.Next(1000);

destination.position.y=num;

num = random.Next(1000);

destination.position.z=num;

What this does is generate a random number then assign it to X, then do it again for Y, and then Z so you get a random XYZ.

A position is made up of three coordinates (x, y and z) represented by a Vector3 data structure. Unity provides an API called Random that can generate a random number between two values. If we use this API to generate random new values for the x, y and z properties of the Game Object’s position, the Game Object will effectively have moved to a new position. Your challenge will be scripting correctly to choose the range values for the random numbers such that the bounds of the values are where you want them … otherwise you could teleport your character to an offscreen position and never find him again.

See:

http://unity3d.com/support/documentation/ScriptReference/Random.Range.html

Bingo! I’m not sure if it was changing it from Random.Range to Random.RandomRange, or if I just needed to tweak the values to fit my map size/location, but the following did the trick perfectly. It still tends to favor a certain side of the map, but not nearly as much.

var destination : Vector3;

function OnTriggerEnter(other : Collider) { 
	if (other.tag == "EvilFace") 
		{ 
		destination = Vector3(Random.RandomRange(-500,1000), Random.RandomRange(2,10),Random.RandomRange(-500,1000));
		other.transform.position = destination; 
		audio.Play(); 
		}

}