- Home /
How to change scenes using box collider and first person controller?
I have a simple scene with a cube in the middle. I want to make it so that when my first person controller runs into the cube the game will automatically transition to the next level.
I made the cube a box collider and checked "is trigger"
I made this script:
var target : Collider;
function OnTriggerEnter(cubeTrigger : Collider)
{
if (cubeTrigger == target)
{ Application.LoadLevel ("longhouserealm");
}
}
and attached it to the cube. (longhouserealm is the name of the scene that i want to go to)
Under the cube's trigger script window, I assigned the First Person Controller as the target.
I have both of the scenes in my Build Settings list.
Buttttt it doesn't work. When i walk through the cube, nothing happens. The only thing that changed is that the cube is now penetrable instead of solid.
I've been trying to figure it out by patching together tutorials all day... I'm a newbie and would appreciate any advice or alternate methods for changing scenes!! Thank you!!!!!
Is your firstPersonController a characterController? Because those don't use physics.
Answer by AlucardJay · Feb 01, 2012 at 01:19 AM
i would suggest start using tags. you can have different trigger events based on the name of the tag. Click on your prefab/object door . In the Inspector at the top you should see Tag with a drop-menu next to it (possibly saying Untagged). Click on dropbox, at the bottom Click on Add Tag ...
In the Tag Manager, expand the Tag menu (click bkack arrow). In the next empty space, add your tag.
i.e. if there is no writing next to Element 0, click on it and write Door .
OK so you made a new tag , now you have to put it on the door. again click on door prefab/object. now in the inspector click on tag, now Door should be on the list. Click on it, the door should now have the tag Door.
Use this in your OnTriggerEnter function :
function OnTriggerEnter (cubeTrigger : Collider)
{
Debug.Log ("OnTriggerEnter : cubeTrigger.tag = " + cubeTrigger.tag); // shows the tag of the trigger
// if tag is door
if (cubeTrigger.tag == "Door")
{
Application.LoadLevel ("longhouserealm");
}
}
if you have the same script running on different scenes (with the player), you are going to need lots of door tags (to load the right scene) . i.e.
if (cubeTrigger.tag == "DoorToRealm")
{
Application.LoadLevel ("longhouserealm");
}
if (cubeTrigger.tag == "DoorToHut")
{
Application.LoadLevel ("hut");
}
if (cubeTrigger.tag == "DoorToCastle")
{
Application.LoadLevel ("castle");
}
great thank you, it's working now! thanks for such a detailed response!!
I'm kinda new to this website and not sure if I'm suppose to be asking this here! So sorry in advance if I'm not!!!! I'm trying to do this SA$$anonymous$$E thing and its not working. No matter what script I use. I have a box collider set up so that when my character runs into it would switch scenes... but he just walks right through, nothing happens. if someone could help me with script? my box collider is set up on a "cube". my scene I want to change to is called "darkwoods" and my character is called "Player". I have my trigger box checked. I tagged my cube as cube and my Player as Player... I do not have a rigitbody. I did add a capsule as my character by the camera and tried that but still luck.