Sunday, June 26, 2011

Unity GUI Level Transition

This idea captured from one of the forums===========================================


create a space trading game and would like help with creating a script to allow the player to choose when to orbit a planet.


Main Algorithm


===========================================================================

var levelToLoad;
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag("Player")) {
Application.LoadLevel("PlanetRotationTest");}}
This will load a new scene when the player approaches the trigger, however I would like instead for a button to appear on the GUI that the player can select when the wish to enter orbit. Any ideas?
=============================================================================
Algorithm for Answer
===================================================================================================================================================================
  1. Copy the scripts into separate files.
  2. Create a sphere. Remove the mesh renderer component. Set the collider to trigger, and scale it slightly larger than the planet. Then center it to the planet.
  3. Add the first script the the trigger, and the second to the player.
=============================================================================

Here's the script for the collider:
function OnTriggerEnter (other : Collider) {
if (other.CompareTag ("Player")) {
SendMessageUpwards ("SetClose");
}
}




function OnTriggerExit (other : Collider) {
if (other.CompareTag ("Player")) {
SendMessageUpwards ("SetFar");
}
}
Put this script on your player:
var close = false;

function SetClose () {
close = true;
}

function SetFar () {
close = false;
}

function OnGUI () {
if (close) {
if (GUI.Button (Rect (10,10,150,100), "I am a button")) {
Application.LoadLevel("PlanetRotationTest");
}
}
}
This will load that level when you click the button and you are near the planet. That's what the if statement is for and the trigger collider is for. Hope this helps!

No comments:

Post a Comment