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 gaboumafou · Sep 15, 2010 at 11:19 PM · triggerdamagedelayinvulnerability

Temporary invulnerability in a platformer

Hello,

I am working on a 2.5D platformer and we are in the finishing stages.

However, there is a feature I never understood how to implement (it's the first time I tried coding and I have zero programming formation).

We want to have a temporary invulnerability after the character takes a hit (like in the Mario games) so that life can't be drained in a second by continuous hits.

We are using "damage" trigger zones. How could I add a condition so that the trigger is only effective if there hasn't been damage in the last X seconds?

Here is the damage script:

var sound : AudioClip;

function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "Player"){ audio.PlayOneShot (sound); LifeGUI.charge--; } }

Many thanks in advance. Please remmember that I am not a programmer, so that what seems obvious to you isn't to me! ;)

Comment
Add comment · Show 1
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 · Sep 15, 2010 at 11:58 PM 0
Share

When posting code, please format it using the code button (icon with 0s and 1s). $$anonymous$$akes it easier to copy/paste/alter!

1 Reply

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

Answer by · Sep 16, 2010 at 12:01 AM

You could use WaitForSeconds in conjunction with a boolean to create a timeout delay where damage cannot be applied. For example:

var sound : AudioClip; var damageTimeout : float = 1.0; private var canDamage : boolean = true;

function OnTriggerEnter (other : Collider) { if(other.gameObject.tag == "Player") { if ( canDamage ) // if damage can be applied ApplyDamage(); // apply the damage } }

function ApplyDamage () { audio.PlayOneShot (sound); // play the sound LifeGUI.charge--; // subtract the life charge canDamage = false; // disallow further damage yield WaitForSeconds(damageTimeout); // wait for the specified damage timeout canDamage = true; // allow damage again }

Any questions, please ask.

Comment
Add comment · Show 3 · 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 gaboumafou · Sep 16, 2010 at 12:39 AM 0
Share

Wow, this look excellent. However, Unity is giving me errors I don't understand with your script:

  1. It's asking for a ";" after the ApplyDamage function

  2. It's giving me the following error on line 18 (LifeGUI.charge--;):

DamageDelay.js(16,29): BCE0044: expecting :, found ';'.

I really don't understand these errors, this seems against all I've learned in the last days about scripting.

Sorry if the answer is obvious, I feel dumb now...

avatar image · Sep 16, 2010 at 01:31 AM 0
Share

Oops, I forgot to declare the function - can't test from where I am, sorry! Updated the answer, should be fine now.

avatar image gaboumafou · Sep 16, 2010 at 05:05 PM 0
Share

Thanks! This seems obvious now, but I was totally clueless yesterday. Well, I should get more confortable with Javascript someday...!

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

Collision, tags, and triggers 1 Answer

Delay audio collisions 1 Answer

Changing State of Character Model on Trigger 0 Answers

How can I make an enemy hurt the player? 3 Answers

How to create/fix fire damage script???? 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