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 BinKill-Ethical · Aug 24, 2014 at 07:55 PM · damage

Damage Every Second Not Working

Hello,

Now before you complain about me not googling or something.

http://puu.sh/b6fKa/faca2505aa.png

It's not worked.

 function Update () {
     if(alive){
         if(character_detect){
             ai_character.LookAt(character);
             var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
             if(distance > 5.0){
                 ai_character.animation.CrossFade("walk01");
                 transform.Translate(Vector3.forward * Time.deltaTime * 3);
             } else {
                 ai_character.animation.CrossFade("attack01");
                 invoke();
             }
         } else {
             ai_character.animation.CrossFade("idle");
         }
     }
 }

 function invoke(){
     InvokeRepeating("attack", 3.0, 1.0);
 }

 function attack(){
     character.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }

The script on my Character object

 function ApplyDamage(damage : int){
     health -= damage;
     if(health <= 0){
         die();
     }
 }
 
 function die(){
     Destroy(character.gameObject);
     Application.LoadLevel("Death_Screen");
 }

Now what my problem is, is not that it's not apply damage. It's applying damage. However it's applying damage every frame. This is getting quite frustrating as I've been working on this for 3 hours now and have yet to find a working solution. I've tried many different things.

What I want my script to do is Apply damage to my character every 3 seconds if I'm within a certain area. Why the hell is this not working?

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
0

Answer by Addyarb · Aug 24, 2014 at 08:15 PM

Haven't tested this, but it seems you need an IEnumerator somewhere in the mix. Try this?

 function Update () {
     if(alive){
         if(character_detect){
             ai_character.LookAt(character);
             var distance = Vector3.Distance(ai_character.transform.position, character.transform.position);
             if(distance > 5.0){
                 ai_character.animation.CrossFade("walk01");
                 transform.Translate(Vector3.forward * Time.deltaTime * 3);
             } else {
                 ai_character.animation.CrossFade("attack01");
                 invoke();
             }
         } else {
             ai_character.animation.CrossFade("idle");
         }
     }
 }
 
 
 
 IEnumerator invoke(){
         while (character_detect) {
                         character.SendMessage ("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
                         yield return new WaitForSeconds (3.0f);
                 }
     
 }
 }


Comment
Add comment · Show 1 · 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 BinKill-Ethical · Aug 24, 2014 at 08:50 PM 0
Share

Is there an IEnumerator equivalent in javascript?

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html

I looked at the JS -> C# comparison and the only difference was it turns IEnumerator into function and when I run that code it still deducts health every frame. I could convert the entire script into C# but I'd prefer to try and avoid doing that.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Structuring a damage and kill count method... 1 Answer

A node in a childnode? 1 Answer

Loadlevel and buttons... 2 Answers

Access to variables from other script objects 3 Answers

Launched from a browser 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