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 Entyro · Aug 20, 2014 at 11:08 AM · ontriggerenterdamageontriggerstay

Damage rate HELP

Hi!

I'm working on a damage script for the player, but I'm having a problem. Now when you walk on a specific trigger your player loses health and that works like it should. But what I want to implement is that when you stay in the trigger box you should lose health every second or two.

I've tried OnTriggerStay, but it wont work like I want it to. I think it calls it every frame, so the player loses all his health in like 1 second.

Here's my script:

 var damage  = 10;
 var healthScript : Health;
 var damageSound: AudioClip;
 //Disable objects
 var pauseScript : Pause;
 var deathScreen : boolean;
 var deathSound: AudioClip;
 var PlayerObjects : GameObject;
 var flashlightObjects : GameObject;
 var cameraObjects : GameObject;
 var interactionObjects : GameObject;
 
 function OnTriggerEnter ()
 {
     healthScript.healthPoints -= damage;
     AudioSource.PlayClipAtPoint(damageSound, transform.position);
 //Death
     if (healthScript.healthPoints <= 0)
     {
         AudioSource.PlayClipAtPoint(deathSound, transform.position);
         healthScript.healthPoints = 0;
         deathScreen = true;
         Time.timeScale = 0;
         AudioListener.pause = true;
         Screen.showCursor = true;
         Screen.lockCursor = false;
 //Disable components
         PlayerObjects.GetComponent(CharacterMotor).enabled = false;
         PlayerObjects.GetComponent(SmoothMouseLook).enabled = false;
         cameraObjects.GetComponent(SmoothMouseLook).enabled = false;
         PlayerObjects.GetComponent(Footsteps).enabled = false;
         PlayerObjects.GetComponent(Crosshair).enabled = false;
          flashlightObjects.GetComponent(Flashlight2).enabled = false;
          PlayerObjects.GetComponent(KeyInventory).enabled = false;
          PlayerObjects.GetComponent(BatteryInventory).enabled = false;
          PlayerObjects.GetComponent(HealthInventory).enabled = false;
         pauseScript.enabled = false;
         interactionObjects.SetActive(false);
         
         Destroy(gameObject);
     }
 }
 
 function OnTriggerStay ()
 {
     healthScript.healthPoints -= damage;
     
     if (healthScript.healthPoints <= 0)
     {
         AudioSource.PlayClipAtPoint(deathSound, transform.position);
         healthScript.healthPoints = 0;
         deathScreen = true;
         Time.timeScale = 0;
         AudioListener.pause = true;
         Screen.showCursor = true;
         Screen.lockCursor = false;
 //Disable components
         PlayerObjects.GetComponent(CharacterMotor).enabled = false;
         PlayerObjects.GetComponent(SmoothMouseLook).enabled = false;
         cameraObjects.GetComponent(SmoothMouseLook).enabled = false;
         PlayerObjects.GetComponent(Footsteps).enabled = false;
         PlayerObjects.GetComponent(Crosshair).enabled = false;
          flashlightObjects.GetComponent(Flashlight2).enabled = false;
          PlayerObjects.GetComponent(KeyInventory).enabled = false;
          PlayerObjects.GetComponent(BatteryInventory).enabled = false;
          PlayerObjects.GetComponent(HealthInventory).enabled = false;
         pauseScript.enabled = false;
         interactionObjects.SetActive(false);
         
         Destroy(gameObject);
     }
 }


Does anyone know how to fix this?

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 Alessio89 · Aug 20, 2014 at 11:16 AM

It's actually pretty easy: you can either use a timer like so:

 var damageTimer : float;
 
     function OnTriggerStay ()
     {
         damageTimer += Time.deltaTime;
         if (damageTimer > [theDelayYouWantHere])
         {
             healthScript.healthPoints -= damage;
             damageTimer = 0;
         }
     
     }

Or you can use a CoRoutine. Now I'm not familiar with how CoRoutine are handled in javascript, but you can check the documentation for that. If I recall correctly, it's easier in Javascript than it is in C# to use coroutines. :)

Here's the link to the docs: CoRoutines

Comment
Add comment · Show 7 · 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 Entyro · Aug 20, 2014 at 02:30 PM 0
Share

Awesome thanks! Works perfectly :D

avatar image Entyro · Aug 20, 2014 at 02:41 PM 0
Share

One more question! :) A problem I have now is when I walk in the trigger a damage sound plays + the OnTriggerStay sound also. So it plays these two sounds the same time so it sounds kinda weird. Do you know how to prevent this? I want a sound playing when you hit it, then when you stay in the trigger the sound keeps playing.

 function OnTriggerEnter ()
 {
     healthScript.healthPoints -= damage;
     AudioSource.PlayClipAtPoint(damageSound, transform.position);
 }
 
 function OnTriggerStay ()
 {
     damageTimer += Time.deltaTime;
     
     if (damageTimer > 1)
     {
         healthScript.healthPoints -= damage;
         damageTimer = 0;
         AudioSource.PlayClipAtPoint(damageSound, transform.position);
     }
 }
 
 function OnTriggerExit ()
 {
     damageTimer = 0;
 }
avatar image Alessio89 · Aug 20, 2014 at 03:04 PM 0
Share

$$anonymous$$ake sure that the damageTimer is set to 0 before entering the trigger.

Also you could keep a bool to check if the player has entered just now the trigger. You can do this: keep a bool entered and set it to true in the OnTriggerEnter. You can then check in OnTriggerStay if this bool is true. If it is you just walked into the trigger, so don't play the damage sound. You then start counting with the timer, at the end of the timer you just check again if entered is true. If it is, set it to off again. Something like this:

 var firstStepInTrigger = false;
 function OnTriggerEnter ()
 {
     firstStepInTrigger = true;
     healthScript.healthPoints -= damage;
     AudioSource.PlayClipAtPoint(damageSound, transform.position);
 }
  
 function OnTriggerStay ()
 {
     damageTimer += Time.deltaTime;
  
     if (damageTimer > 1)
     {
         healthScript.healthPoints -= damage;
         damageTimer = 0;
         if (firstStepInTrigger)
         {
            firstStepInTrigger = false;
         }
         else
         {
            AudioSource.PlayClipAtPoint(damageSound, transform.position);
         }
     }
 }
  
 function OnTriggerExit ()
 {
     damageTimer = 0;
     firstStepInTrigger = false;
 
 }

avatar image Entyro · Aug 20, 2014 at 03:15 PM 0
Share

Well it works, kinda. When I just walk on it it works, when I stay on it, the first damage sequence doesn't have any sound, but the rest of them have it. Do you know why? Sorry for all the questions, I'm really grateful that you're helping me! :)

avatar image Alessio89 · Aug 20, 2014 at 03:39 PM 0
Share

try to replace

  if (firstStepInTrigger)
         {
            firstStepInTrigger = false;
         }
         else
         {
            AudioSource.PlayClipAtPoint(damageSound, transform.position);
         }

with

  if (firstStepInTrigger)
         {
            firstStepInTrigger = false;
         }
         AudioSource.PlayClipAtPoint(damageSound, transform.position);


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

22 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

Related Questions

Multiple Cars not working 1 Answer

Problem with OnTriggerEnter() 1 Answer

Damage problem 4 Answers

OnTriggerStay/Enter/Exit 2 Answers

Problem with enemy health. 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