C
C#2y ago
c_ccc

OnTriggerEnter - When player is in zone for 3 seconds, take damage Help

Sorry, I technically posted this a minute ago but due to a radical change in my script, I decided to entirely repost it: I tested the player damage in the ontriggerenter and it works. The ontriggerexit also works. why is the countdown not working?
public class DeathRegion : MonoBehaviour
{
public float countDown = 3f;
float zoneTime;
public GameObject player;
public ControllerTest2 PLcontroller;

void Start()
{
zoneTime = countDown;
//PLcontroller = GameObject.FindObjectOfType<ControllerTest2>();
}

public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
zoneTime -= Time.deltaTime;
Debug.Log("COUNTDOWN START");
}
}

void Update()
{
if (zoneTime <= 0f)
{
PLcontroller.takeDamage(100);
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
zoneTime = countDown;
Debug.Log("Survived");
}

}
}
public class DeathRegion : MonoBehaviour
{
public float countDown = 3f;
float zoneTime;
public GameObject player;
public ControllerTest2 PLcontroller;

void Start()
{
zoneTime = countDown;
//PLcontroller = GameObject.FindObjectOfType<ControllerTest2>();
}

public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
zoneTime -= Time.deltaTime;
Debug.Log("COUNTDOWN START");
}
}

void Update()
{
if (zoneTime <= 0f)
{
PLcontroller.takeDamage(100);
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
zoneTime = countDown;
Debug.Log("Survived");
}

}
}
5 Replies
Steak
Steak2y ago
onTrigger only happens once there 2 solution for this start timer using bool executed on update, turn it on/off on enter/exit OR use onTriggerStay
c_ccc
c_ccc2y ago
oh shit you are right
Steak
Steak2y ago
it happens each enter
c_ccc
c_ccc2y ago
that was a really obvious mistake thanks for pointing that out
Steak
Steak2y ago
btw u may want to use Overlap instead on Fixedupdate its not dependent on unity frame