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 IKilledKenny_2 · Mar 14, 2015 at 06:34 AM · javascriptcollisiononcollisionenter

OnCollisionEnter Problem

Hello Unity3D.i have a problem with my script.The Problem with my script is,My OnCollisionEnter won;t work the way i want it.For example,I have 5 animations i want my character to do on an opponent and i only does 1 out of the 5.Which is the first one...If anyone knows how i can make all 5 of the animations work.Can you please tell me how?

Heres the script

 var player : GameObject;
 
 
   
     
 
 function OnCollisionEnter(collision : Collision){
  
  if(collision.gameObject.tag == "Dummy"){
   if(player.animation.IsPlaying("Sweep_Kick"))
 
 collision.transform.gameObject.animation.Play ("Stand_Air_Hit_back");
 
 }
 else
 if(collision.gameObject.tag == "Dummy"){
  if(player.animation.IsPlaying("Violets_Bicycle_Kick"))
 collision.transform.gameObject.animation.Play("Violets_Bicycle_Kick_Hit_Part_One");
 }
 else
 if(collision.gameObject.tag == "Dummy"){
  if(player.animation.IsPlaying("Violets_Bicycle_Kick_Part_2"))
 collision.transform.gameObject.animation.Play("Violets_Bicycle_Kick_Hit_Part_Two");
 }
 else
 if(collision.gameObject.tag == "Dummy"){
  if(player.animation.IsPlaying("Lance_Kick_2"))
 collision.transform.gameObject.animation.Play ("Air_Hit_Going_Up");
 }
 else
 if(collision.gameObject.tag == "Dummy"){
  if(player.animation.IsPlaying("Falcon_Punch"))
 collision.transform.gameObject.animation.Play ("Hit_Flying");
 
 
  
      
          
     }
  }
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 Priyanshu · Mar 14, 2015 at 07:00 AM 1
Share

logic problem. control will never go out of first "dummy" "if", if u are always checking against dummy check against animations inside one "dummy" if.

avatar image Beennn · Mar 14, 2015 at 07:07 AM 1
Share

You only need one "if(collision.gameObject.tag == "Dummy")" to wrap everything. Alternatively (though very ugly) you could remove the else's.

avatar image IKilledKenny_2 · Mar 14, 2015 at 08:12 AM 0
Share

Im sorry.Do you mean like this?Because i tried this way as well and there was no difference.

 var player : GameObject;
 
 
   
     
 
 function OnCollisionEnter(collision : Collision){
  
  if(collision.gameObject.tag == "Dummy"){
   if(player.animation.IsPlaying("Sweep_$$anonymous$$ick"))
 
 collision.transform.gameObject.animation.Play ("Stand_Air_Hit_back");
 
 }
 else
  if(player.animation.IsPlaying("Violets_Bicycle_$$anonymous$$ick")){
 collision.transform.gameObject.animation.Play("Violets_Bicycle_$$anonymous$$ick_Hit_Part_One");
 }
 else
  if(player.animation.IsPlaying("Violets_Bicycle_$$anonymous$$ick_Part_2")){
 collision.transform.gameObject.animation.Play("Violets_Bicycle_$$anonymous$$ick_Hit_Part_Two");
 }
 else
  if(player.animation.IsPlaying("Lance_$$anonymous$$ick_2")){
 collision.transform.gameObject.animation.Play ("Air_Hit_Going_Up");
 }
 else
  if(player.animation.IsPlaying("Falcon_Punch")){
 collision.transform.gameObject.animation.Play ("Hit_Flying");
 
 
  
      
          
     }
  }

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by raulrsd · Mar 15, 2015 at 02:28 AM

I think it's a problem with the way you're organizing the if statements. Try this code instead:

 function OnCollisionEnter(collision : Collision){
         
         if(collision.gameObject.tag == "Dummy"){
             if(player.animation.IsPlaying("Sweep_Kick")){
                 collision.transform.gameObject.animation.Play ("Stand_Air_Hit_back");
             }
             else if(player.animation.IsPlaying("Violets_Bicycle_Kick")){
                 collision.transform.gameObject.animation.Play("Violets_Bicycle_Kick_Hit_Part_One");
             }
             else if(player.animation.IsPlaying("Violets_Bicycle_Kick_Part_2")){
                 collision.transform.gameObject.animation.Play("Violets_Bicycle_Kick_Hit_Part_Two");
             }
             else if(player.animation.IsPlaying("Lance_Kick_2")){
                 collision.transform.gameObject.animation.Play ("Air_Hit_Going_Up");
             }
             else if(player.animation.IsPlaying("Falcon_Punch")){
                 collision.transform.gameObject.animation.Play ("Hit_Flying");
             }
         }    
     }

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 IKilledKenny_2 · Mar 15, 2015 at 01:25 PM 0
Share

This way works =D.But this also works aswell

 var player : GameObject;
 
 
   
     
 
 function OnCollisionEnter(collision : Collision){
  
  if(collision.gameObject.tag == "Dummy"){
   if(player.animation.IsPlaying("Sweep_$$anonymous$$ick"))
 collision.transform.gameObject.animation.Play ("Stand_Air_Hit_back");
 }
 if(player.animation.IsPlaying("Violets_Bicycle_$$anonymous$$ick")){
 collision.transform.gameObject.animation.Play("Violets_Bicycle_$$anonymous$$ick_Hit_Part_One");
 }
  if(player.animation.IsPlaying("Violets_Bicycle_$$anonymous$$ick_Part_2")){
 collision.transform.gameObject.animation.Play("Violets_Bicycle_$$anonymous$$ick_Hit_Part_Two");
 }
  if(player.animation.IsPlaying("Lance_$$anonymous$$ick_2")){
 collision.transform.gameObject.animation.Play("Air_Hit_Going_Up");
 }
  if(player.animation.IsPlaying("Falcon_Punch")){
 collision.transform.gameObject.animation.Play("Hit_Flying");
 
 
  
      
          
     }
  }
avatar image raulrsd · Mar 15, 2015 at 01:51 PM 0
Share

The problem with your solution is that the if statements which are out of if(collision.gameObject.tag == "Dummy") will be fired even if the GameObject that is colliding has a tag different from "Dummy". I don't know if you are ok with that behavior.

avatar image IKilledKenny_2 · Mar 15, 2015 at 02:18 PM 0
Share

No I just wanted with one tag

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

23 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

Related Questions

OnCollisionEnter not working. 2 Answers

Checking to see if two objects are colliding. 2 Answers

Cannot remove a prefab using OnCollisionEnter function. 1 Answer

function OnCollosionEnter problems 1 Answer

Collide detection with tag [JS] 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