- Home /
DIsabling a Box Collider Trigger after cut scene
So I currently have a 2D Box Collider with the is trigger option checked. Once the player enters that area a new scene (the battle scene where battling takes place) is loaded. Once the battle scene ends and I load the previous scene, since the player is still on the spot of the trigger it activates again, loading the battle scene again.
This creates a continuous loop. I'm not sure where to put the code to disable this or if there is a better method to handling location triggered cutscenes/fight scenes.
I have a global game state script that handles the states of the game. Here is an example of my state machine code:
void Awake(){
DontDestroyOnLoad (transform.gameObject);
if (instance == null) {
instance = this as GameStateMachine;
} else {
Destroy (gameObject);
}
gameState = GameState.PROCESSING;
}
// Update is called once per frame
void Update () {
switch (gameState) {
case(GameState.WORLD):
//Didint Work
collider = GameObject.Find ("StartBattle");
Debug.Log ("collider" + collider);
if (zero == 1) {
if (collider) {
Debug.Log ("turning off collider");
collider.SetActive (false);
}
}
//Didnt Work ^^
SceneManager.LoadScene("main");
gameState = GameState.PROCESSING;
break;
case(GameState.TITLESCREEN):
gameState = GameState.PROCESSING;
break;
case(GameState.BATTLE):
Debug.Log ("Is working");
gameState = GameState.PROCESSING;
zero++;
SceneManager.LoadScene("Battle");
break;
case(GameState.PROCESSING):
break;
}
}
Answer by TheDJBuntin · Jul 03, 2017 at 08:09 PM
Ok, so the simple answer to your question is to put this whereever you want the it to stop being the trigger:
GetComponent</*WhateverColliderYouAreUsing*/>().isTrigger = false;
Something I noticed in your code which you should learn and fix immediately is: You do "GameObject.Find(...)" in Update(), this is very very bad because the game will be searching through all game objects for one with this name every single frame Have this set in Start() instead so it only needs to be found once.
Answer by Raimi · Jul 04, 2017 at 12:43 AM
Your state machine doesn't look well formed. For one you shouldn't have it all in update. You should use events and delegates. This way you run state specific code when an event is triggered, like on state change, rather than checking state every frame.
public enum States {SceneEnter, SceneExit, ScenePause, ScenePlay, GameOver};
public States state;
public States lastState;
public delegate void OnStateChange ();
public static event OnStateChange EnterScene;
public static event OnStateChange ExitScene;
public static event OnStateChange PlayScene;
public static event OnStateChange PauseScene;
public static event OnStateChange GameOver;
public void SetState(States newState)
{
lastState = state;
state = newState;
switch (state)
{
case States.SceneEnter:
EnterScene ();
break;
case States.SceneExit:
ExitScene ();
break;
case States.ScenePlay:
PlayScene ();
break;
case States.ScenePause:
PauseScene ();
break;
case States.GameOver:
GameOver ();
break;
}
}
Your other scripts can subscribe to events and only run code when a specific event has occurred on state change. When you call SetState in this example it will store the last state, change the state to new state and fire an event based on the state. Your code thats subscribed to the event will be triggered and run code specific to that event. Only subscribed scripts will be trigger by that event.
Sorry this is a bit convoluted, but state machines can be very complex and powerful and hard to explain in a short answer.
There is lots of info on events a delegates out there, please research them, they are very powerful. Again Im very sorry I cant explain better, I want to help but finding it hard to explain in a straight forward way.