Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

On collision change level?

Discussion in 'Scripting' started by acloudyskye, Apr 29, 2014.

  1. acloudyskye

    acloudyskye

    Joined:
    Feb 2, 2014
    Posts:
    2
    Hi, so I've been working on a game project and I need to get a script that changes the level when the player that is controlled by the standard third person controller. now be prepared as I Know very little about javascript and i'm still trying to learn. heres what I have so far that doesn't work

    var endlevel = false;
    var player : Rigidbody;

    function Update () {
    if.(Collision.player)
    set var endlevel = true;
    }

    Any help?
     
  2. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    1. Attach a collider to your "End of Level Trigger" object.
    2. Tick the "Is Trigger" option for the collider.
    3. Attach a custom script like the following.
    Assets/EndOfLevelTrigger.js
    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. // Specify name of next level using inspector!
    5. var nextLevelName : String;
    6.  
    7. function OnTriggerEnter(other : Collider) {
    8.     if (other.CompareTag('Player')) {
    9.         // Player hit "End of Level Trigger" object!!!
    10.         Application.LoadLevel(nextLevelName);
    11.     }
    12. }
    13.  
     
    Last edited: Apr 29, 2014
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Please use codetags : [co.de] your code [/co.de] without the dots.

    There is a function for the CharacterController called OnControllerCollisionHit, for rigidbodies OnCollisionEnter and some other similar ones

    Code (csharp):
    1.  
    2.  
    3.  
    4. function OnControllerColliderHit ( other : ControllerColliderHit )
    5. {
    6.      if ( other.gameObject.tag == "yourTag")
    7.      {
    8.            Application.LoadLevel( 0 ); // or any other level index, if it's always the next one just put Application.loadedLevel +1 as parameter
    9.      }
    10. }
    11.  
    12.  
    Also have a look at the basic functions that you can inherit from MonoBehaviour http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html
     
  4. acloudyskye

    acloudyskye

    Joined:
    Feb 2, 2014
    Posts:
    2
    Thank you for the help :) I'll try the script when I'm back on my computer.