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
1
Question by Sayuka · Jun 29, 2014 at 05:04 PM · c#coroutinedamagemelee

Melee-hits being recognized too often

I added a melee system to my 2.5D sidescroller, so far, the melee itself works fine, though for some reason, the enemies get instantly destroyed instead of just being damaged.

My Script for hits-taken in the Enemy-script looks like this:

     public void EnemyDeath() 
     {
         Player pstats = Player.GetComponent<Player> ();
         isHit = true;
         bloodfront.renderer.enabled = true;
         bloodback.renderer.enabled = true;
         collider.enabled = false;
         pstats.CurrentEXP += 40;
         Destroy (gameObject, 0.5f); 
     }
 
     public void DisCheck(float dist)
     {
         Debug.Log (dist);
         if (dist < 2) 
         {
             EnHealth -= 25;
             isHit = true;
             StartCoroutine (WaitForNextHit ());
         }
     }
 
     bool hittable = true;
     bool defeated = false;
 
     private IEnumerator WaitForNextHit() {
         yield return new WaitForSeconds(0.1f);
         hittable = true;
         isHit = false;
         Debug.Log("WaitForNextHit Done");
     }
 
     void Update () 
     {
 
         float hitting = 0.1f;
         Player pstats = Player.GetComponent<Player> ();
         float distx = transform.position.x - pstats.transform.position.x;
         float disty = transform.position.y - pstats.transform.position.y;
 
         if (pstats.attackingLeft && distx > -3 && distx < 0 && hittable)
         {
             hittable = false;
             DisCheck (disty);
         }
 
         if (pstats.attackingRight && distx < 3 && distx > 0 && hittable)
         {
             hittable = false;
             DisCheck (disty);
         }
 
         if (EnHealth <= 0 && !defeated) 
         {
             defeated = true;
             EnemyDeath ();
         }
 
         if (!isHit) 
         {
             hitting = 0.1f;
             transform.position = Vector3.MoveTowards (transform.position, Waypoints [Marker].transform.position, 2 * Time.deltaTime);    
         
             if (transform.position == Waypoints [Marker].transform.position) 
             {
                 Marker ++;
             }
             if (Marker == Waypoints.Length) 
             {
                 Marker = 0;    
             }
         }
 
         if (isHit) 
         {
             hitting = 1.0f;    
         }
 
 
     }

With the bool "defeated" I prevented the player from getting the experience multiple times. What I want is for the enemy to loose health on a hit just once and be immune to hits for a short moment. Though once the player attacks the enemy, it's always a direct kill and I don't know why.

The "IsHit" and "hitting" are for the being hit animation of the enemy, it's not fully working, but for that I'll open a different question, since it's a different issue.

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
3
Best Answer

Answer by Juice-Tin · Jun 29, 2014 at 08:59 PM

Here's an easy way to fix this using an ID:

On enemy

 public uint hitID;

On sword/Player

 //Class variable
 uint attackID;
 
 //When sword is swung
 attackID = Random.Range(0,uint.MaxValue);
 
 //When hitting an enemy
 if(enemy.hitID != attackID){
 // Hit enemy
 enemy.hitID = attackID
 }



The way this works, swinging your weapon gives it a random ID. Hitting an enemy gives them that same ID. If the enemy already has that ID, then the sword knows it was already struck, and won't hit the enemy twice in 1 swing. :)

Comment
Add comment · Show 2 · 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 Sayuka · Jun 30, 2014 at 08:59 AM 1
Share

Thanks, that worked! well, I have that test in the enemy and therefor made the attackID public. Also had to cast the Random.Range part to uint, but it works just fine now.

avatar image Log0n · Jul 21, 2018 at 02:35 PM 0
Share

Thank you $$anonymous$$r. Juice! Genius!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a fill take exactly n seconds to complete 2 Answers

Move to point script - bad OOP and coroutines 0 Answers

Armour / Damage System RPG-like? 2 Answers

Distribute terrain in zones 3 Answers

Unexpected Co-Routine Behavior 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