#hi, i'm trying to make lava in my game but their is a bug

3 messages · Page 1 of 1 (latest)

robust orchid
#

i really don't know how to make the lava damage the player constantly it just damages the player once, i tried to fix it but couldn't.

here's my code:
https://tenor.com/view/aaaaaa-gif-14892085788746462862
using UnityEngine;
public class Lava : MonoBehaviour
{
public float damage;

private void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("Player"))
    {
        other.GetComponent<player_stats>().TakeDamage(damage);
    }
}

}

crude apex
# robust orchid i really don't know how to make the lava damage the player constantly it just da...

I assume you want to take continuous damage?
you could for example use
https://docs.unity3d.com/6000.0/Documentation/ScriptReference/MonoBehaviour.OnTriggerStay.html
and apply the damage every frame as long as you're inside the lava
since this method should be called every physics frame, you can multiply the damage with deltaTime (Time.fixedDeltaTime and Time.deltaTime both work for this, as deltaTime will be fixedDeltaTime when be used inside a physics context... at least i think that might be true for the OnX methods too and not just the FixedUpdate, but to be safe)
other.GetComponent<player_stat>().TakeDamage(damage * Time.fixedDeltaTime);

this would apply the damage you set over the course of a second
if you want to reduce the health only in certain intervals, like a second, you have to look into using timers

there might be some better ways but i think that's an alright beginner approach