- Home /
How do I get the MainCamera to trigger application.loadLevel when it hits a collider?
Hi all,
I am working on my first game in Unity and at the start I have a camera moving around the starting area which I want to trigger the main level to begin once it gets to a specific point.
The code I am using is...
function OnTriggerEnter (Collision : Collider)
{
if(Collision.gameObject.tag == "MainCamera")
{
Application.LoadLevel("level2");
}
}
I have attached this script to an EmptyGameObject with a Trigger Collider, I have animated the MainCamera to enter this trigger box at the end of its cycle. The main camera is tagged as 'MainCamera' to match the tag in the script.
When I do this nothing happens, however if I use a FirstPersonController instead, it works fine, as soon as I walk into the trigger collider the next level loads.
I do not want a FirstPersonController active though, as it is an intro camera pan where I want no interaction from the player.
I have also tried using other GameObjects to trigger the next level, but these do not activate the trigger either, it only seems to be the FirstPersonController that will trigger the next level to load when it hits the collider.
Can anyone offer any advice on this please?
Many thanks in advance.
Answer by hockohock · Aug 03, 2011 at 01:51 AM
My first thoughts, you might want to try if(Collision.tag == "MainCamera") rather than if(Collision.gameObject.tag == "MainCamera"). An easy way to tell what is colliding with your GameObject is by print(Collision.tag) or print(Collision.name). Other than that you might want to double check the tag of the Camera and also make sure there is a collider on there.
Thanks for your advice, I added a Rigidbody to the camera and now it works fine. I thought a Box Collider would be sufficient, I didn't think it would need a Rigidbody aswell, but it seems it does!
Thanks for your help
Answer by aldonaletto · Aug 03, 2011 at 02:25 AM
Triggers detect rigidbodies or CharacterControllers. Create any simple object - a cube, for instance - and add a CharacterController to it using Components/Physics/Character Controller menu. Child the object to the camera, and reset the object's position. Presto! Your camera will be detected by any trigger!
Don't mind about the CharacterController you've added to the camera: it's just a component, not the First Person Controller (the F P Controller is a prefab; it has its own CharacterController, and that's why it activates the trigger).
Thanks for your advice, yes adding a Rigidbody to the camera works just great!
Thanks for the answer. You can also add to the cube (stored as a child of the camera) a rigidbody + collider (if someone don't like to add a character controller). And don't remember to check is$$anonymous$$inematic and uncheck Use Gravity.
Your answer
Follow this Question
Related Questions
how to load random level but one? 3 Answers
Application load advanced loading NO EXPERIENCE 2 Answers
[Unity 3] Music playback stops unexpectedly when loading a new level 2 Answers
Current scene number 2 Answers
How to see what level is running? 2 Answers