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 /
  • Help Room /
avatar image
0
Question by TheSoloPlayer · Nov 03, 2016 at 02:14 PM · unity 5

How do i make a sword only do damage to an enemy when the left mouse button is clicked?

I have a script that whenever the enemy is collided with the sword, it takes 10 damage from the enemy. The problem is that the enemy takes damage just by the character walking into the enemy. I want it so that the enemy can only take damage from the sword when the left mouse button is clicked. How should I do this? This is my code so far:

 var enemyHealth : int;
 
 function Start () {
     enemyHealth = 100;
 }
 
 function OnTriggerEnter(coll : Collider) {
     if(coll.gameObject.tag == "Sword"){
             enemyHealth = enemyHealth-10;
             Debug.Log("Enemy Hit with Sword and health is" + enemyHealth);
         }
     
 }
 
 function Update () {
     if(enemyHealth <= 0)
     {
         Destroy(GameObject.FindWithTag("enemy"));
         Debug.Log("Enemy Died");
     }
 }
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 Zodiarc · Nov 03, 2016 at 02:18 PM

 function OnTriggerEnter(coll : Collider) {
      if(coll.gameObject.tag == "Sword" && Input.GetKeyDown(KeyCode.Mouse0)){
          enemyHealth = enemyHealth-10;
          Debug.Log("Enemy Hit with Sword and health is" + enemyHealth);
      }


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 TheSoloPlayer · Nov 05, 2016 at 03:44 AM 0
Share

Usually, when i don't add && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0) whenever the sword collided with the enemy, it took damage from the enemy, when I added that to the script however, no matter if I clicked the Left $$anonymous$$ouse Button, it still wouldn't take damage from the enemy. It's strange cause you would think that it could work but it just doesn't seem to work.

avatar image TheSoloPlayer · Nov 05, 2016 at 05:20 PM 0
Share

Update: The script does make the enemy take damage but the problem is that you need to rapidly click the left mouse button for the enemy to take damage. I want it so that the animation on the sword could play, and during that period of time, the enemy would take damage. What do I add to the script so that the enemy could take damage from a single mouse click, but lengthen that time to say 3 seconds?

avatar image PizzaPie · Nov 05, 2016 at 07:48 PM 0
Share

Play the animation first with the Input.Get$$anonymous$$ouseButtonDown condition or whatever it fits and then detect if it hits any enemy colliders. If i am not mistaken you run that script on the enemy and not on the player. You got to do it the other way around.To avoid hitting anything while you are not attacking try to deactevate the trigger or the collider on the sword or try a condition that checks if the animation is playing i am not sure if there is a direct way to do it like bool : animation.isPlaying or something like this.

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

unity 5 car traffic system 0 Answers

How to reference script in unity (C#) ? 0 Answers

Unity 2d Collision Glitch Help 0 Answers

Rotation animation keyframes problem 1 Answer

How to decreased the visible particles 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