Animation Issues - Won't play OnTriggerExit
Hi! I'm making a game in Unity, where the player has to get to a location / room before a timer reaches zero.
I'm using Unity 2020.1.0f1.
Each location / room (I'll just call it room from now on), has a door. This door has an animation that is controlled by a script, DoorAnim, which uses OnTriggerEnter and OnTriggerExit to control these animations.
What's supposed to happen is:
The player goes up to the door.
The player collides with a Box Trigger Collider, which opens the door.
The player continues through the door, exiting the collider.
In the room, right after the door, there is another trigger collider.
The player walks into this collider, triggering an animation that turns the camera around to face the door.
The door closing animation is triggered when the player exits the door collider, so they can see the door closing as the camera turn-around animation is being played.
The winning screen is shown.
This works for the first level I have made of my game.
However, for my next level, I can't seem to get it to work.
The door opening animation works fine, however the door closing animation won't play. I've spent a couple days trying to get this to work but I just can't figure it out, so that's why I need help here.
I'm definitely not a Game Programmer, I'm much more focused on level design. I'd say 95% of the code in these scripts were written from a tutorial. I'm sure there is redundant code in these scripts, or things that could be much simplified. If anyone has any suggestions for these, that would be helpful, too.
Code For door animation (DoorAnim):
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class DoorAnim : MonoBehaviour
{
[SerializeField] private Animator myAnimationController;
public enum scenes { Level_01, Airplane };
public scenes myScene;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
switch (myScene)
{
case scenes.Level_01:
Debug.Log("Level_01");
myAnimationController.SetBool("playanim", true);
break;
case scenes.Airplane:
Debug.Log("Airplane");
myAnimationController.SetBool("playanim4", true);
break;
default:
Debug.Log("Inncorrect Scene Name Error");
break;
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player") || other.CompareTag("End_Cam"))
{
switch (myScene)
{
case scenes.Level_01:
Debug.Log("Level_01");
myAnimationController.SetBool("playanim", false);
break;
case scenes.Airplane:
Debug.Log("Airplane");
myAnimationController.SetBool("playanim4", false);
break;
default:
Debug.Log("Inncorrect Scene Name Error");
break;
}
}
}
}
Code that handles the winning of the level (PanelScript):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.SceneManagement;
public class PanelScript : MonoBehaviour
{
public static float time;
public GameObject toilet;
public GameObject finishPanel;
public TextMeshProUGUI finishTime;
public TextMeshProUGUI finishStatus;
public static bool finish = false;
public static bool active = false;
public GameObject playerCam;
public GameObject animCam;
public AudioClip sound;
AudioSource audioSource;
public float volume = 1f;
private string sceneName;
public enum scenes {Level_01, Airplane};
public scenes myScene;
[SerializeField] private Animator myAnimationController;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
// Create a temporary reference to the current scene.
Scene currentScene = SceneManager.GetActiveScene();
// Retrieve the name of this scene.
string sceneName = currentScene.name;
}
private void OnTriggerEnter(Collider other)
{
if (time >= 0.0f && other.tag == "Player")
{
finishPanel.SetActive(true);
finishTime.text = time.ToString();
finish = true;
finishStatus.text = "LEVEL CLEAR!";
playerCam.SetActive(false);
animCam.SetActive(true);
audioSource.PlayOneShot(sound, 1F);
active = true;
Debug.Log("Panel Active True");
switch (myScene)
{
case scenes.Level_01:
Debug.Log("Level_01");
myAnimationController.SetBool("playanim2", true);
break;
case scenes.Airplane:
Debug.Log("Airplane");
myAnimationController.SetBool("playanim3", true);
break;
default:
Debug.Log("Inncorrect Scene Name Error");
break;
}
}
}
// Update is called once per frame
void Update()
{
time = Timer.timer;
if (time == 0.0f && finish == false)
{
finishPanel.SetActive(true);
finishTime.text = "D.N.F.";
finishStatus.text = "LEVEL FAILED!";
}
}
}
If anyone needs any more info, I'll be able to provide more.
Thanks!
Your answer
Follow this Question
Related Questions
Animation on Collision 1 Answer
ANIMATION TRIGGER DOESN'T PLAY 1 Answer
How to modify coliders during animation 0 Answers
Help with OnTriggerEnter Not working correctly? 1 Answer
Rotate a hinge joint on its own 0 Answers