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 /
This question was closed Apr 21, 2017 at 02:42 AM by Ben_Huang4163 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ben_Huang4163 · Apr 17, 2017 at 09:14 AM · collidercollision detectionweapon systemattackingontrigger

weapon collider

Hi: I'm trying to make the player attacking an enemy by his weapon during the player's attack animation, i use a collider which attached to player's weapon to detect the enemy . i found that the enemy takes the damage twice, i'm not sure this happened it is because the enemy have two colliders or what. what i want is the enemy will take damage only if the enemy's capsule collider is touched by player's weapon and at a specific frame of the player attack animation. the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SwordAttack2 : MonoBehaviour {
 
     private int _playerAttackStateHash = Animator.StringToHash("Base Layer.Attack2");
     Animator _animator;
     public int swordDamage = 30;
     RaycastHit hit;
     GameObject enemy;
     EnemyHealth enemyHealth;
     bool enemyinRange = false;
 
     void Awake () {
         _animator = GetComponentInParent<Animator>();
         enemy = GameObject.FindGameObjectWithTag("Enemy");
         enemyHealth = enemy.GetComponent<EnemyHealth>();
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Enemy"))
         {
             enemyinRange = true;
             AnimatorStateInfo info = _animator.GetCurrentAnimatorStateInfo(0);
             if (info.nameHash == _playerAttackStateHash && enemyinRange == true)
             {
                 enemyHealth.TakeDamage(swordDamage, hit);
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         if (other.gameObject == enemy)
         {
             enemyinRange = false;
         }
     }
 }

alt text

alt text

if anyone can help me with this, please? thank you!

sword-collide.jpg (32.8 kB)
enemy-collider.jpg (91.3 kB)
Comment
Add comment · Show 2
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 MarshallN · Apr 17, 2017 at 05:49 PM 2
Share

Try adding a float invulnTime; to the EnemyHealth script; during the Update() of enemyHealth, have invulnTime -= Time.deltaTime;; and when TakeDamage is called, have a check like

 if( invulnTime >= 0 )
 {
     invulnTime = 0.5f; // Or however many seconds you want the enemy to be invulnerable for during the attack.
     // Run all your damage scripts;
 }

This'll let you know if it's hitting multiple times because it's hitting multiple colliders in a short amount of time.

avatar image Ben_Huang4163 MarshallN · Apr 18, 2017 at 08:17 AM 0
Share

Thank you ! It works great! but just one little problem, i think the script should be like this :

 takedamage(){
  invulnTime = 0.5f; 
  if( invulnTime >= 0 )
  {
     
      // Run  damage scripts;
  }
 }
 

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by Davirtuoso · Apr 18, 2017 at 10:35 AM

In your OnTriggerEnter method, add a check that checks the type of collider it is impacting and wrap your current code within it. Only run the code if the type is capsule collider, otherwise ignore. Something like this might work at the start (untested):

 if (other.GetType() == CapsuleCollider)
 {
     // your current code here
 }

I'm afraid I haven't used the animator much so I can't help with the specific-frame part of your question, though hopefully you'll be able to find that out/figure that part out. I hope this helps :)

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 Ben_Huang4163 · Apr 18, 2017 at 12:06 PM 0
Share

Thank you! that is another good way to do the job. i didn't know how to deal with multiple colliders before you show me. it helps.

avatar image Davirtuoso Ben_Huang4163 · Apr 23, 2017 at 06:08 PM 0
Share

Not a problem, i'm glad it helped you. Good luck with your development!

Follow this Question

Answers Answers and Comments

79 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

Related Questions

Collision not work with my level,Colliders not work with my 3d level 1 Answer

How to decrease the speed of the player by some fraction after triggering the box collider? 2 Answers

Raycast occasionally won't hit colliders on Mecanim animated characters (but hits perfectly if Animator is disabled) 1 Answer

Raycast passes Collider 0 Answers

OnCollisionEnter sensitivity 2 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