• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tubestorm · May 14, 2020 at 04:02 PM · ui3ddamageover time

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tubestorm · May 14, 2020 at 07:50 PM 0
Share

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)'
 
avatar image Tubestorm · May 14, 2020 at 07:59 PM 1
Share

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;

avatar image icehex Tubestorm · May 14, 2020 at 09:27 PM 0
Share

Awesome - thanks for catching that! Updated the answer for completeness.

avatar image icehex Tubestorm · May 14, 2020 at 09:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

249 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

WorldToScreenPoint UI element showing up twice 1 Answer

3D GameObject not visible in UI 1 Answer

Render Texture vs 3D model in screen space - camera 1 Answer

Is it better to use 3D text or a UI canvas in world space? 1 Answer

Changing Toggle UI's value (Checked or Unchecked) 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges