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
0
Question by axcher · Jul 11, 2014 at 06:32 PM · animationcollisionsendmessagecollisiondetection

Assist with CollisionDetection

Hello everybody! Could you please help with making the SendMessage function (BTW Is it right to call it a function?) work once per animation even if there were numerous collisions. Or how can I make the collisions to be undetectable for a certain amount of time?

Sorry if there are mistakes, English is a foreign language to me.

 #pragma strict
 
 var otherObj : GameObject;
 var Damage : int = 50;
 
 function OnCollisionEnter (hit : Collision)
 {
     if(otherObj.animation["Attack"].enabled)
     {
         if(hit.gameObject.tag == "enemy")
         {
             hit.gameObject.SendMessage("ApplyDamage", Damage);
         }    
     }
 }
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
Best Answer

Answer by Tehnique · Jul 11, 2014 at 06:48 PM

You can check if the animation is playing using

 if (!animation.IsPlaying("Attack"))
 {
 ...
 }

Just ignore the collisions while Attack is playing.

It should work with enabled also, but in both cases you should use a var to check if you sent the message. Something like:

 var otherObj : GameObject;
 var Damage : int = 50;
 var hasSentAttackMessage: bool = false;
  
 function OnCollisionEnter (hit : Collision)
 {
     if(otherObj.animation["Attack"].enabled && !hasSentAttackMessage)
     {
         if(hit.gameObject.tag == "enemy")
         {
             hit.gameObject.SendMessage("ApplyDamage", Damage);
             hasSentAttackMessage = true;
         }   
     }
     else if(!otherObj.animation["Attack"].enabled)
     {
         hasSentAttackMessage = false;
     }
 }

Comment
Add comment · Show 7 · 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 axcher · Jul 11, 2014 at 07:13 PM 0
Share

For some reason with this code the damage is not applied at all. I'm not sure if this is what I need because the problem is that when I attack(sword hit) and, for example, drag the mouse Unity detects many collisions and the enemy takes the damage few times.

avatar image Tehnique · Jul 11, 2014 at 07:24 PM 0
Share

Well, if you really want to disable collision for the animation duration, you can call

Physics.IgnoreCollision(player_collider, enemy_collider, true);

then make a method that enables the collision back (using false in the above line), and call this method using Invoke after x seconds (where x is animation duration). Ex, for 2 sec: Invoke("EnableCollision", 2);

avatar image axcher · Jul 11, 2014 at 07:37 PM 0
Share

Could you please type the whole code because I am new to all this and I don't understand many things you might find obvious. I would be very grateful!

avatar image Tehnique · Jul 11, 2014 at 07:46 PM 0
Share

Something like:

 var otherObj : GameObject;
 var Damage : int = 50;
 var attackDuration : int = 2;
  
 function OnCollisionEnter (hit : Collision)
 {
     if(otherObj.animation["Attack"].enabled)
     {
         if(hit.gameObject.tag == "enemy")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", Damage);
             Physics.IgnoreCollision(otherObj.collider , transform.gameObject.collider, true);
             Invoke("EnableCollision", attackDuration );
         }   
     }
 }
 
 function EnableCollision()
 {
     Physics.IgnoreCollision(otherObj.collider , transform.gameObject.collider, false);
 }



I'd still try to use the var that tells if you sent the message, as in my initial answer, it's less resource intensive.

avatar image axcher · Jul 11, 2014 at 08:16 PM 0
Share

Unfortunately, it still detects many collisions. Well, thanks, anyway, that was very informative. I guess I need to try something else, there must be easier ways than the one I chose.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Pixel Perfection collision on 3d animated object 0 Answers

How to make my player play a vault animation when i collide into a vaultable object? 1 Answer

Im NOT Switching Scenes!! PLEASE HELP!! 1 Answer

Collisions on Models... 2 Answers

How to get the first person controller to collide with an animated object? 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