Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges