Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BrettRiet · Apr 03, 2011 at 05:02 AM · triggertimedamagerate

Rate of health loss in trigger box.

so i have a script that makes it when i go in the box area i loos 10 hp points but i dont know how to make it happen every second longer again if i dont leave the box. i can't figure out how to make my Time script part.

var Damage = 10; var Rate = 1;

function OnTriggerEnter (col : Collider) { var player : CharacterDamage = col.GetComponent(CharacterDamage);

     player.ApplyDamage(Damage);

}

function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }

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 KeithK · Apr 03, 2011 at 06:54 AM

You could do the intial damage like you're doing now. Then for the timed damage that occurs while you stay in the box try this.

It uses the OnTriggerStay event, which is fired continuously after OnTriggerEnter, and until OnTriggerExit. You keep track of the total time in RateCount by adding the delta time value of Time until it equals or exceeds the Rate you want, apply the damange, reset the count.

var Damage = 10; var Rate : float = 1.0f; var RateCount : float = 0.0f;

function OnTriggerEnter (col : Collider) { var player : CharacterDamage = col.GetComponent(CharacterDamage);

 player.ApplyDamage(Damage);
 RateCount = 0;

}

function OnTriggerStay (col : Collider) { RateCount += Time.deltaTime;

if (RateCount >= Rate){ var player : CharacterDamage = col.GetComponent(CharacterDamage); player.ApplyDamage(Damage);

    RateCount = 0.0f;

    print("Fire!");

} }

Open up your console in Unity Editor and see if you get that print("Fire") appearing.

Hope this helps. :)

Comment
Add comment · Show 8 · 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 BrettRiet · Apr 03, 2011 at 07:04 AM 0
Share

At the part where it says "RateCount += Timer.deltaTime;".... i am no expert at scripting but is it a typo, should it really be Time, no Timer. but even that way the script is not working and i will work on this hard but i could use any suggestions you might know

avatar image KeithK · Apr 03, 2011 at 08:03 AM 0
Share

Ah sorry, I'll fix that. It should indeed be Time and not Timer. Hmm that is strange, the timed damage should be firing correctly. I'm not JavaScript guru though, I write my scripts in C#. So it may well be something funny in my JS syntax that's not working. I'll change the answer code, and you can give that a try.

avatar image KeithK · Apr 03, 2011 at 08:10 AM 0
Share

The only thing I'm certain of here is the logic that makes the code in the OnTriggerStay if statement fire once every Rate. Stick a print() in the if statement and check me on that though.

avatar image BrettRiet · Apr 03, 2011 at 02:19 PM 0
Share

I think i could figure this out if new what Time.deltaTime is, i kind of know what it is but i can't implement it into any if my scripts cause i don't know how to use it and i THIN$$anonymous$$ we could use it some where in the (RateCount >= Rate) but im not sure how that would work..... maybe not?

avatar image KeithK · Apr 03, 2011 at 03:14 PM 0
Share

Time.delaTime is the time that has passed since the last frame. Tell me what there is to figure out in this script above? What isn't working?

Show more comments

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

OnTriggerStay Problem with rate? 1 Answer

Does a mesh collider animate? 0 Answers

How to create an Idle counter ? 2 Answers

Collision, tags, and triggers 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