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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED :) ] Unity Scene Transition with Triggers help

Discussion in 'Scripting' started by namdo, May 2, 2015.

  1. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Hey guys, So I'm here with another problem.

    I have 2 characters in my game that you can switch with a button.

    I want to be able to transition to the 2nd scene after both characters are touching their triggers.

    This is my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class StageTransitionOne : MonoBehaviour {
    7.  
    8.     public bool SceneTransitionOne = false;
    9.     public bool SceneTransitionTwo = false;
    10. //    public GameObject OtherSwitch;
    11.  
    12.  
    13.     void Start()
    14.     {
    15. //        OtherSwitch.GetComponent<StageCompleteOne>();
    16.     }
    17.  
    18.  
    19.  
    20.     void OnTriggerEnter (Collider collis)
    21.     {
    22.  
    23.         Debug.Log ("Collided with: " + gameObject.tag);  // important debugging aid!
    24.  
    25.  
    26.         if ((gameObject.tag == "TarroStageOne"))
    27.         {
    28.             SceneTransitionOne = true;
    29.         }
    30.  
    31.        
    32.         if ((gameObject.tag == "KeannaStageOne"))
    33.         {
    34.             SceneTransitionTwo = true;
    35.         }
    36.  
    37.         if ((SceneTransitionOne == true) && (SceneTransitionTwo == true))
    38.         {
    39.             Application.LoadLevel("SceneTwo");
    40.         }
    41.  
    42.  
    43.     }
    44.  
    45. }
    I posted a problem like this before that I wasn't able to fix.

    Can anyone help me with this as its alot smaller than what i was asking before.

    thank you
     
  2. Aidy

    Aidy

    Joined:
    Oct 15, 2014
    Posts:
    19
    What's happening exactly, is it not triggering?

    If you haven't already, you should debug the code. Put a Debug.Log() in each of if statements controlling the detection to ensure that it's actually being picked up first.
     
  3. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    No it is triggering. What I'm trying to do is.

    Each character has their own trigger. I want both characters to reach their trigger before the scene transition. Like if they 2 keys to open a gate.

    Character 1 gets his key, character 2 gets her key then they open a gate. I want character 1 to know that character 2 has the key and vice versa.

    So I want character 1 to know that character 2 has collided with her trigger and vice versa.
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Hmmm... Looking at your example, I'd suggest a couple of alterations. First, let's get rid of the unnecessary double parenthesis. Next, let's move the check to see if both bools return true from OnTriggerEnter to Update so that it's processed as soon as it happens. I'm not certain this will solve your problem, but it couldn't hurt to try :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StageTransitionOne : MonoBehaviour
    5. {  
    6.     public bool SceneTransitionOne = false;
    7.     public bool SceneTransitionTwo = false;
    8.     private int ticks = 0; // simple counter (I use this for everything)
    9.  
    10.     void Update()
    11.     {
    12.         if (SceneTransitionOne == true && SceneTransitionTwo == true && ticks == 0)
    13.         {
    14.             ticks++; // add 1 to our ticks counter so that this code is only processed once
    15.             Application.LoadLevel("SceneTwo");
    16.         }
    17.     }
    18.  
    19.     void OnTriggerEnter (Collider collis)
    20.     {          
    21.         Debug.Log ("Collided with: " + gameObject.tag);  // important debugging aid!
    22.         if (gameObject.tag == "TarroStageOne")
    23.         {
    24.             SceneTransitionOne = true;
    25.         }
    26.         if (gameObject.tag == "KeannaStageOne")
    27.         {
    28.             SceneTransitionTwo = true;
    29.         }                      
    30.     }      
    31. }
     
  5. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Thanks for helping, but it didn't solve my problem. Both triggers aren't communicating with each other. Trigger 1 doesnt know Trigger 2 has been hit.
     
  6. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Oh geez... I think I know what the issue is, but let me clarify to be sure... I was under the impression that this code was on a single object that both characters were interacting with... If there are two separate objects, and each character touches a different one, then both values will never be true since both characters aren't touching the same trigger. In that case, you'll need to have one trigger tell the other trigger (through BroadcastMessage or whatever interscript communication option you prefer) that it's value has been set to true. I can work up a quick example if you need.
     
  7. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Yeah they're on 2 different triggers. I'll really appreciate an example. \

    The characters are on 2 different sides of a stage. When one gets to their trigger, I dont want him to be able to leave the stage until the 2nd character gets to their trigger.

    They both have to get to their goals before exiting the stage. Hence 2 different triggers.
     
  8. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    I think the following approach may work for you. You would put the script on your two trigger objects, NOT on the characters! In the Unity inspector, for each of the trigger objects you would specify the validPlayer string that this particular trigger is intended for.

    Code (CSharp):
    1. public class StageTransitionOne : MonoBehaviour
    2. {
    3.     public bool triggered = false;
    4.     public string validPlayer = "";
    5.     private StageTransitionOne[] allTransitionScripts;
    6.     void Start()
    7.     {
    8.         allTransitionScripts = FindObjectsOfType(typeof(StageTransitionOne)) as StageTransitionOne[];
    9.     }
    10.  
    11.     void OnTriggerEnter (Collider collis)
    12.     {
    13.         if ((collis.tag == validPlayer))
    14.         {
    15.             triggered = true;
    16.         }
    17.         foreach (StageTransitionOne currentScript in allTransitionScripts)
    18.         {
    19.             if (!currentScript.triggered)
    20.                 return;
    21.         }
    22.        
    23.         Application.LoadLevel("SceneTwo");
    24.     }
    25. }
     
  9. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    See how this works for ya... Put this on both triggers.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StageTransitionOne : MonoBehaviour
    5. {
    6.     public GameObject trigger1; // assign in the Inspector
    7.     public GameObject trigger2; // assign in the Inspector
    8.     public bool SceneTransitionOne = false;
    9.     public bool SceneTransitionTwo = false;
    10.     private int ticks = 0; // simple counter (I use this for everything)
    11.    
    12.     void Update()
    13.     {
    14.         if (SceneTransitionOne == true && SceneTransitionTwo == true && ticks == 0 && this.gameObject == trigger1) // added this last part so that both triggers don't try to load the new level at the same time. Only trigger 1 will switch scenes
    15.         {
    16.             ticks++; // add 1 to our ticks counter so that this code is only processed once
    17.             Application.LoadLevel("SceneTwo");
    18.         }
    19.     }
    20.    
    21.     void OnTriggerEnter (Collider collis)
    22.     {        
    23.         Debug.Log ("Collided with: " + gameObject.tag);  // important debugging aid!
    24.         if (gameObject.tag == "TarroStageOne")
    25.         {
    26.             SceneTransitionOne = true;
    27.             if(this.gameObject == trigger1) // if this is trigger #1,
    28.             {
    29.                 trigger2.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #2 what happend.
    30.             }
    31.             if(this.gameObject == trigger2) // if this is trigger #2,
    32.             {
    33.                 trigger1.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #1 what happend.
    34.             }
    35.         }
    36.         if (gameObject.tag == "KeannaStageOne")
    37.         {
    38.             SceneTransitionTwo = true;
    39.             if(this.gameObject == trigger1) // if this is trigger #1,
    40.             {
    41.                 trigger2.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #2 what happend
    42.             }
    43.             if(this.gameObject == trigger2) // if this is trigger #2,
    44.             {
    45.                 trigger1.BroadcastMessage("OtherTrigger", gameObject.tag); // tell trigger #1 what happend
    46.             }
    47.         }                    
    48.     }
    49.  
    50.     public void OtherTrigger(string tag) // this gets called by the "other" trigger in the scene
    51.     {
    52.         if(tag == "TarroStageOne")
    53.         {
    54.             SceneTransitionOne = true;
    55.         }
    56.         if (tag == "KeannaStageOne")
    57.         {
    58.             SceneTransitionTwo = true;
    59.         }
    60.     }
    61. }
    62.  
     
  10. CG_Echtzeitschmiede

    CG_Echtzeitschmiede

    Joined:
    Feb 19, 2015
    Posts:
    93
    Also, tag comparisons should be done with CompareTag, not with tag ==

    So line 13. should be:
    if (collis.CompareTag(validPlayer))
     
  11. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    LOL! Looks like CG_Echtzeitschmiede beat me to the punch ;)
     
  12. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    @krougeau
    IT WORKS!!!!1 Thank you so much!!!! Thank you so much!!! Thank you!!

    I'm still a beginner, so I'm just getting the hang of things. I'm gonna study the code so I dont get lost again. Thank you.

    @CG_Echtzeitschmiede
    Thanks for helping. It's works too. :)

    Thank you guys. I'm so happy.
     
    krougeau likes this.
  13. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Awesome, glad it worked out ;) Have fun!