Damage dealt over time question, Need Help

Hey!

I am trying to deal damage to my player each time they move.
my player moves with a joystick. this is a mobile app type game for ios.
it is a 3d game. coded in c#.

does anyone have a way for me to achieve this.


i’m not really sure how to do this, as the destination of the character is basicially arbitary, and tied to where ever the player moves.


Any scripts i can use to get this feature would be much appreciated.

Hi, just whipped this up on the spot, so untested and also I don’t know what variables you have. But this should give you all the basics you need.

private Vector3 last_pos;
private float health;
private float damage;
bool walking_in_lava;

void Start()
{
    health = 100;
    damage = 10; //this will turn into 10 damage per second per unit of distance
    last_pos = transform.position;
    walking_in_lava = true;
}

void FixedUpdate()
{
    if (walking_in_lava)
    {
        health -= damage * (Vector3.Distance(transform.position, last_pos)) * Time.fixedDeltaTime;            
        last_pos = transform.position;
    }
}