Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Geoffroditis · Apr 12, 2019 at 02:20 PM · swordattackingenemydamage

Having problem with sword attacking enemy every frame and instantly killing him

Basically I have an attack script for my player to attack a goblin with a sword, but every time he hits him, It instantly kills the enemy.

I tried making a bool that would only allow damage to be taken 1 time, but nothing happened. anyone have any ideas what happened?

Here's my script (This was put on the player) :

 public int playerHealth = 15;
 public int playerDamage = 2;
 bool AttackTimer = true;

 void OnCollisionEnter(Collision col)
 {
     if (col.transform.tag == "EnemySword" && AttackTimer == true)
     {
         playerHealth -= 2;
         AttackTimer = false;
         Debug.Log(AttackTimer);
         if (playerHealth <= 0)
         {
             Destroy(gameObject);
         }
     }
 }

 void OnCollisionExit(Collision col)
 {
     AttackTimer = true;
 }

 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }

}

Comment
Add comment · Show 8
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 metalted · Apr 12, 2019 at 03:17 PM 0
Share

If the players is killed instantly, this must mean that the OnCollisionEnter() is called multiple times, because this is the only place where the health is reduced. Try removing the OnCollisionExit() function and see if it still happens. It could be something in the range of: The player gets hit with a collider, the two colliders bounce a bit. This causes the collider to exit again, turning the variable AttackTimer true again. If the collider is still in motion, it will hit again and reduce the health once more. This process will repeat until the player object is destroyed).


So try to remove line 21: AttackTimer = true; The variable should never turn to true that way. If it is still happening, there something else going on.

avatar image Geoffroditis metalted · Apr 16, 2019 at 02:45 PM 0
Share

@metalted Thanks for responding, but it seems that when I remove OnCollisionExit, the enemies now won't take damage at all. I think that the collides could be bouncing off each other, but this doesn't seem to be the way to solve it. Could it have to do with the collier on my sword? I'm using a mesh collider.

avatar image KevRev · Apr 16, 2019 at 05:05 PM 0
Share

In your OnCollisionEnter add this line before the if loop:

 Debug.log ("Hit by: "+col.transform.name+". Tag:"+col.transform.tag);

This will confirm the name of the object and tag that is colliding with the player, whilst also confir$$anonymous$$g that the collision has actually been detected.

Sometimes you'll have unexpected results here (like the collision is actually an arm or hand).

If the tag matches "EnemySword" you know there's either a problem with the bool state stopping it from starting the IF loop.

If you're still struggling, post the console log so we can see what it's doing.

I'd also add the following as the first line in your inner loops:

 Debug.log ("Starting attack loop");
 
 Debug.log ("Triggered collisionExit loop");

These will help you debug if the routines are being triggered as expected.

avatar image Geoffroditis KevRev · Apr 18, 2019 at 02:14 PM 0
Share

@$$anonymous$$evRev @metalted talted Hey thanks, but I've tried that and I now seem to get an error "unityengine.Debug does not contain a definition for log"

Also I've updated the PlayerHealth script a bit it now looks like this:

using System.Collections; using UnityEngine.Experimental.UIElements; using System.Collections.Generic; using UnityEngine;

public class PlayerHealth : $$anonymous$$onoBehaviour {

 public int playerHealth = 15;
 public int playerDamage = 2;
 public float flashSpeed = 5f;
 public Image damageImage;
 public Enemy$$anonymous$$ovement enemy;
 bool didSwing = false;

 void OnCollisionEnter(Collision col)
 {
     Debug.log ("Hit by: " + col.transform.name + ". Tag:" + col.transform.tag);
     if (col.transform.tag == "EnemySword" && didSwing == true)
     {
         enemy.enemyHealth -= 2;
         if (enemy.enemyHealth <= 0)
         {
             Destroy(gameObject);
         }

         didSwing = false;
     }
 }

}

$$anonymous$$y swing script (allows the player to swing) looks like this:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class swing : $$anonymous$$onoBehaviour { Animator anim; bool didSwing = false; public Enemy$$anonymous$$ovement enemy;

 void Start()
 {
     //get the animator
     anim = GetComponent<Animator>();

 }

 // Update is called once per frame
 void Update()
 {
     //call triggers via keypress - you can also assign them via the preset inputs
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
     {
         anim.SetTrigger("swingA");
         didSwing = true;
         Debug.Log("swapTrue");
     }
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse1))
     {
         anim.SetTrigger("swingB");
         didSwing = true;
         Debug.Log("swapTrue");
     }
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.W))
         anim.SetTrigger("Run");
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         anim.SetTrigger("Jump");


 }

 void OnCollisionEnter(Collision col)
 {
     if (col.transform.tag == "EnemySword" && didSwing == true)
     {
         enemy.enemyHealth -= 2;
         if (enemy.enemyHealth <= 0)
         {
             Destroy(gameObject);
         }

         didSwing = false;
     }
 }

}

Thanks for the help though guys

avatar image KevRev Geoffroditis · Apr 18, 2019 at 02:32 PM 0
Share

$$anonymous$$ake sure you have the right case for the word Log after Debug.

The additional debug lines will confirm what's happening when. You really need to see those to be able to continue troubleshooting.

Fix the debug lines and let us know the results.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

103 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 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 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 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 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

Programming a Sword Swing Animation and Swapping weapons 1 Answer

Different Enemy Lives JavaScript 1 Answer

First person sword fighting 1 Answer

Animations: just making sure my workflow is right. 1 Answer

Procedural Sword Fighting on a mobile device? 0 Answers


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