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 30, 2019 at 01:49 PM · playerdeathenemy damagehealth

Player Will not take any damage!

I've been working on a project for a while now, and have tried to implement the ability for the player take damage, and nothing works! I have done the exact same thing to my enemies and it works for them to take damage and die, but the player won't take any damage. Please help, thanks.

PlayerHealth Script:

 public int playerHealth = 15;
 public int playerDamage = 2;
 public float flashSpeed = 5f;
 public Image damageImage;
 public EnemyMovement enemy;
 bool damaged;
 public Color flashColour = new Color(1f, 0f, 0f, 0.1f);

 void OnCollisionEnter(Collision col)
 {
     Debug.Log("EnterCollider");
     if (col.transform.tag == "EnemySword")
     {

         damaged = true;
         playerHealth -= 3;
         Debug.Log("DamageTaken");
         if (playerHealth <= 0)
         {
             Destroy(gameObject);
         }
         damaged = false;
     }
 }

 void Update()
 {
     //If the player has just been damaged...
     if (damaged)
     {
         // ... set the colour of the damageImage to the flash colour.
         damageImage.color = flashColour;
         Debug.Log("FlashTrue");
     }
     // Otherwise...
     else
     {
         // ... transition the colour back to clear.
         damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
     }

     // Reset the damaged flag.
     damaged = false;
 }

}

Swing Script: { Animator anim; bool didSwing = false; public EnemyMovement enemy; AudioSource playerAudio; public AudioClip SwordSwing;

 void Start()
 {
     //get the animator
     anim = GetComponent<Animator>();
     GetComponent<AudioSource>();
     GameObject.FindGameObjectsWithTag("Enemy");

 }

 // Update is called once per frame
 void Update()
 {
     //call triggers via keypress - you can also assign them via the preset inputs
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         anim.SetTrigger("swingA");
         didSwing = true;
         Debug.Log("swapTrue");
         //playerAudio.clip = SwordSwing;
         //playerAudio.Play();
     }
     if (Input.GetKeyDown(KeyCode.Mouse1))
     {
         anim.SetTrigger("swingB");
         didSwing = true;
         Debug.Log("swapTrue");
         //playerAudio.clip = SwordSwing;
         //playerAudio.Play();
     }
     if (Input.GetKeyDown(KeyCode.W))
         anim.SetTrigger("Run");
     if (Input.GetKeyDown(KeyCode.Space))
         anim.SetTrigger("Jump");

 }

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

}

EnemyMovement Scipt:

{

 public Transform Player;
 public int enemyHealth = 10;
 public int enemyDamage = 3;
 public int MoveSpeed = 4;
 public int MaxDist = 10;
 public float MinDist = 1f;
 bool didSwing = false;
 public PlayerHealth player;


 Animator anim;

 void Start()
 {
     //get the animator
     anim = GetComponent<Animator>();
     GameObject.FindGameObjectsWithTag("Sword");

 }

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

         didSwing = false;
     }
 }

   
 void Update()
 {
     transform.LookAt(Player);

     if (Vector3.Distance(transform.position, Player.position) >= MinDist)
     {

         transform.position += transform.forward * MoveSpeed * Time.deltaTime;
         anim.Play("Run");


         if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
         {
           
             
             anim.SetTrigger("swingA");
             didSwing = true;
             //Debug.Log("EnemySwingATrue");

             anim.SetTrigger("swingB");
             didSwing = true;
             //Debug.Log("EnemySwingBTrue");

         }

     }
     

 }

}

The debug logs within the Health script tell me that the enemy sword is entering the collider, but no damage is being taken.

Comment
Add comment · Show 3
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 Casiell · Apr 30, 2019 at 02:14 PM 1
Share

So you get the "EnterCollider" log. But do you also get the "DamageTaken" log? If not, then you have wrong tag on your enemy sword

avatar image metalted · Apr 30, 2019 at 02:40 PM 0
Share

In the documentation they use col.gameobject.tag ins$$anonymous$$d of col.transform.tag. Not sure if it will help because if transform didnt have .tag it would probably give an error. But its worth a try :p

avatar image Larcondos metalted · Apr 30, 2019 at 02:50 PM 0
Share

@metalted col.gameObject.tag and col.transform.tag both respond the same way in this context - I actually opened up my editor to make sure before commenting on that lol

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Larcondos · Apr 30, 2019 at 02:34 PM

As Casiell mentioned, you are getting the EnterCollider log, so that part is working properly. Since (presumably) your tag check is failing, I would suggest first making sure your enemy sword is tagged properly. If it is but you are still receiving this problem, I would add this line of code which will tell you what object is triggering your EnterCollider log.

 print(col.transform.tag);

Since this will print the tag of the object that is actually triggering the event, you might be able to debug it easier.

Comment
Add comment · 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

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

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

How to add Player health and ability to take damage from a cube? 2 Answers

Death scene/screen on player Health Script Wont work! 0 Answers

I need help with the player's health points 1 Answer

Damage script is screwed up...? what to do? 1 Answer

c# how to change player location on 0 hp 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