|
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!
(comments are locked)
|
|
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. 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. 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)
|
|
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.
(comments are locked)
|
|
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
(comments are locked)
|
|
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. @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.
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)
|
