- Home /
I need help making it so my countdown trigger timer doesn't start until the entire rigid body is inside the trigger
Hello,
I have this script for when my boat enters the trigger that after 5 seconds inside it will turn green. However, I need to make it so that the countdown only starts when the entire boat is in the trigger. Can someone please help adapt my script?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Revised : MonoBehaviour {
public GameObject ready, go, comp;
int countDownStartValue=500;
// Use this for initialization
void Start () {
// StartCoroutine(TimeAwait());
}
// Update is called once per frame
//void Update () {
void OnTriggerStay(Collider col){
if (col.gameObject.name == "ready") {
if (countDownStartValue > 0) {
countDownStartValue--;
Invoke ("CountDownTimer", 1.0f);
ready.SetActive (true);
//go.SetActive (false);
}
else if (countDownStartValue <= 0) {
ready.SetActive (false);
go.SetActive (true);
Time.timeScale = 0;
comp.SetActive (true);
//Application.LoadLevel (8);
}
}
}
void OnTriggerExit(Collider col){
if (col.gameObject.name == "ready") {
ready.SetActive (true);
go.SetActive (false);
}
}
/*
IEnumerator TimeAwait(){
yield return new WaitForSeconds(5);
Debug.Log ("KAshan");
}*/
}
Answer by rh_galaxy · Feb 11, 2020 at 08:48 AM
What if you shrink the collider to be the size it needs to be for the ship to always be inside the area you want when touching the collider. That would work if the ship object has a square shape at least. Also I think you should do something like this for the timer...
float countDown;
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name == "ready") {
countDown = 5.0f;
}
}
void OnTriggerStay(Collider col)
{
if (col.gameObject.name == "ready") {
countDown -= time.deltaTime;
if(countDown <= 0) {
//time out reached
//...
}
}
}
Do you have an idea of how i could add that to the current script?
Answer by tormentoarmagedoom · Feb 09, 2020 at 10:47 AM
Hello.
Maybe a good solution can be to create some child objects. Each child with a colliders and a script to detect that all chils are inside. One all are inside, means entire boat is inside.
ok thank you. do you have any idea how I could code something like that
Your answer
Follow this Question
Related Questions
My trigger is called twice 1 Answer
Trigger or Colission not work if inside volume 2 Answers
Detecting Trigger Collision for Mecanim Animator 0 Answers
Trigger script does not work. 1 Answer