- Home /
How to make player keep getting hurt when touching fire?
Here is some background information:
I just learn about Unity and game this month, and I am making a VR game about fire, I was trying to make the player get hurt when they touch fire. I was using a cube to pretend it is the player.
I have try some method online about the Collision between the player and fire to let them get hurt, and I found out that only the last method is working to detect player is touching fire.
When my player touch fire, there is only one log each time, it means I can't track is player is keep in fire or not, so I can't keep decrease the player's HP value. Also, the player need to be very close to let the method detect the player is touching the fire.
Here is my Qusetions:
Is OnTriggerEnter method is the only method I can use? I see the tutorials on the internet are mostly using OnParticleCollision or OnCollisionEnter, should I use those too?
Also, how can I keep track if player is touching fire and decrease player's health?
Lastly, now the player need to be very close to the fire get response of touching the fire. How can I increase the detection area of player touching fire?
Answer by Waterpolo7 · Dec 25, 2021 at 11:33 AM
Hi @s20130789,
I think you should use the OnTriggerStay() method, which will log every frame your player is in the fire. OnTriggerEnter() only logs the single frame the player enters the fire. Decreasing the players health should be something like:
//This is the damage the fire will be doing
[SerializeField] float damage;
void Start(){
//You should add a new script to the player called HealthScript and in that you should add a public float health.
float playerHealth = GameObject.FindGameObjectWithTag("Player2").GetComponent<HealthScript>().health;
}
private void OnTriggerStay(){
//You refer to the players health in the start function of the fire. You just subtract damage * time from the player when he's in the fire. You can adjust the damage in the inspector of the fire.
playerHealth -= damage * Time.deltaTime;
}
Lastly, you can change the size of a collider in the inspector, this should then be matched to the size of the fire itself.
Good luck! :)