- Home /
I'm having a problem with OnTriggerStay2D
Basically I need to make a timer for how long the player is in the circle collider to trigger the various game over functions. The timer function works in void Update() but for some reason not in the OnTriggerStay2D().
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerLose : MonoBehaviour { public Canvas gameOver; public Canvas pause; public Button pauseButton;
public Transform target;
public float speed;
public float contactTime;
public float timer;
public GameObject player;
public CircleCollider2D circle;
void Start()
{
gameOver = gameOver.GetComponent<Canvas> ();
pause = pause.GetComponent<Canvas> ();
pauseButton = pauseButton.GetComponent<Button> ();
}
void Update()
{
timer -= Time.deltaTime;
if (timer < 0)
{
float step = speed * Time.deltaTime;
transform.position = Vector2.Lerp (transform.position, target.position, step);
timer = 0;
}
}
void OnTriggerEnter2D(CircleCollider2D circle)
{
contactTime = 0;
Debug.Log ("PlayerEntCol");
}
void OnTriggerStay2D(CircleCollider2D circle)
{
contactTime += Time.deltaTime;
if(contactTime > 3)
{
Time.timeScale = 0.05f;
gameOver.enabled = true;
Destroy (player);
pause.enabled = false;
pauseButton.enabled = false;
Debug.Log ("PlayerGaOv");
}
}
}
Answer by nadhimali · Jul 26, 2015 at 09:21 AM
I know it sounds stupid but try removing contactTime = 0
from OnTrigerEnter2d()
and see if anything changes.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Work around for onTriggerStay? 1 Answer
C# OnTriggerStay Collider Problem 1 Answer
Multiple Cars not working 1 Answer
How to set a Collider of a GameObject to Inactive similar to the SetActiveMethod 1 Answer