- Home /
How do I make it so that when you pass over a line, you switch levels?
I want to make it so that when you walk between two posts (made with Sketchup), you go to the next level. This is exactly what I wrote so that you can change levels
function Update () { (Application.LoadLevel (1); }
Now what do I add to make it do this when the player touches the object I want them to?
P.S. Full code would be appreciated because I'm new to unity and don't know how to write it properly.
Answer by Uriel_96 · Dec 10, 2010 at 04:04 AM
This is not how it works, what you are doing in theat script is only pass to another level, what you need to do its add the posts a collider and make it trigger and make sure the 2 objects(player and posts) have rigidbody so like that you only write this script in the posts and put the players tag = "player":
function OnTriggerEnter(other : Collider){
if(other.transform.tag == "player"){
Application.LoadLevel (2);
}
}
Note: if the posts are separated one of other make sure to put an empty object and parent the posts into the empty object and in that empty object put this script.
That one of $$anonymous$$C HALO its very helpful to, a little similar but its different. $$anonymous$$y script works for trigger and putting this to the posts and the one of $$anonymous$$C HALO is working with hits and its putted on the player
Yep that's true both will work and Uriel code does what you need it to do as well You have to good answers :)
Answer by MC HALO · Dec 10, 2010 at 04:06 AM
Ok the first thing is make a new object resize it an place it on the floor, once placing the object on the floor Tag it Meaning in unity make a tag and add that tag to the object that you placed on the floor. I would name the tag something like "WalkedOverLevel1" A shit name thats because i can not think of anything else lol. Ok When you do that make sure you player has a character controller on him if so then create a new java script and place the following code :
function OnControllerColliderHit (hit : ControllerColliderHit){
if(hit.gameObject.tag == "WalkedOverLevel1") {
Application.LoadLevel(2);
//Or you can place the level name like so:
// Application.LoadLevel("Grave");
}
}
I prefer the second option this is because if you use the 1 2 3 etc it will cause problems later on. Say for example you moved level 1 and you created a new level before that and that level will now be level one in unity 3d. If you place the name it will load it by looking for the name rather than the numbers.
yep that's all you need to do :)
Answer by Given · Jan 26, 2011 at 02:00 AM
function Update (){ if(transform.position.x <= 12){ Application.LoadLevel(2); } }
Another approach: if you pass a certain point, it loads a new level (I use less than 12 X) the advantage of this is that you don't need to create any colliders or tag anything. Its also very simple.