x


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!

more ▼

asked May 03 '12 at 01:00 AM

Xyvik gravatar image

Xyvik
3 1 1 1

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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(); 
    }
}
more ▼

answered May 03 '12 at 01:29 AM

hijinxbassist gravatar image

hijinxbassist
2k 23 31 38

Tried this one, but it seems to keep teleporting "EvilFace" to the same corner of the map each time. Would it make more sense to try and teleport "EvilFace" to a random "empty" game object from a collection of them?

May 03 '12 at 02:49 AM Xyvik

...and somehow this posted before I was done. I've looked into Arrays but haven't a clue about them yet. Would making, say, seven or eight empty opjects, call them "RandomTeleport", and somehow call one of them randomly for "EvilFace" to teleport to? If that sounds logical (it sort of does to me, maybe...) how would I accomplish that?

May 03 '12 at 02:50 AM Xyvik

And nevermind, figured it out!

May 03 '12 at 03:23 AM Xyvik
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 03 '12 at 01:29 AM

Aggressor gravatar image

Aggressor
20 1 6 7

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 03 '12 at 01:19 AM

kolban gravatar image

kolban
1.8k 2 7

(comments are locked)
10|3000 characters needed characters left

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(); 
       }

}
more ▼

answered May 03 '12 at 05:21 AM

Xyvik gravatar image

Xyvik
3 1 1 1

@Xyvik Every situation will vary and have its own "Magic numbers", it would make sense to have a few different specified points where the "Evil Face" spawns (an array of positions), and use Random.Range(0,myPositionsArray.length) to choose which position to spawn at.

var spawnPositions:Vector3[];

//Set spawn positions in Start or Load them in the Inspector
spawnPositions=GameObject.FindObjectsWithTag("EvilFaceSpawnPosition").transform.position as Vector3[];

function OnTriggerEnter(other : Collider) 
{ 
    if(other.tag == "EvilFace") 
    {
        //Spawn evil face at a random spawn position contained in the spawnPositions array
        other.transform.position=spawnPositions[Random.Range(0,spawnPositions.length-1)];

        audio.Play(); 
    }
}
May 03 '12 at 08:15 AM hijinxbassist

I'm going to try this one as well and see which one I prefer. A million thanks, friend :D

May 03 '12 at 07:48 PM Xyvik
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x572
x90
x80

asked: May 03 '12 at 01:00 AM

Seen: 681 times

Last Updated: May 03 '12 at 07:48 PM