- Home /
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.
Answer by icehex · May 14, 2020 at 05:45 PM
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;
}
}
Hey thank you for responding. I'm a little confused with the line it's referring to (being your line 18) since i can't figure out what that is trying to do.
here is the error I got.
Assets/Scripts/Stats/CharacterStats.cs(107,46): error CS7036: There is no argument given that corresponds to the required formal parameter 'b' of 'Vector3.Distance(Vector3, Vector3)'
Actually thanks it worked perfectly. there was just an error with this line 18. it should be:
health -= damage * (Vector3.Distance(transform.position, last_pos)) * Time.fixedDeltaTime;
Awesome - thanks for catching that! Updated the answer for completeness.
Also a bit of explanation on line 18. We start with asking for a subtraction from health equal to the damage number. Then we scale the damage number by the distance traveled in the last frame in such a way that when you travel 1 full unit, you take damage x 1 = damage amount of damage. If less than 1 unit was travelled, say 50% of 1 unit, then player takes 50% of the damage only, this frame. Same thing with time - if only 2% of one second has passed each update frame, Time.fixedDeltaTime will bump down the total damage to 2% of what it would be. But since time continues to pass, this 2% will be applied 50 times in 1 second, giving you the final math of health -= damage x 1 x 1 if the player travels 1 unit of distance in 1 second. Time.fixedDeltaTime is set in the project settings under Physics, where you can edit the time interval between fixed-length frames. The 2% above would correspond to a fixed frame update time of 0.02 seconds per frame, and Time.fixedDeltaTime would equal 0.02.
Your answer
Follow this Question
Related Questions
How to use two cameras in VR to display 3D object on top of UI panel? 0 Answers
Damage Indicator not showing correct position of attack. 0 Answers
UI mask with a 3D object 1 Answer
Convert 2D Camera to 3D for my Gameplay UI Setup 0 Answers
How can an UI Element know on which 3D Object it is pointing? 1 Answer