• 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 THINK 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.

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

No one has followed this question yet.

Related Questions

OnTriggerStay Problem with rate? 1 Answer

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

Why doesn't OnTriggerEnter2D get called? 1 Answer

Using a trigger collider to detect enemies. 1 Answer

Help with destroying within a certain zone... 1 Answer

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