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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by IKilledKenny_2 · Jan 20, 2015 at 06:47 PM · collisionmovementcollidercharactercontrolleroncollisionenter

OnCollisionEnter

Hello Unity3D.I have a question about OnCollisionEnter?If i want OnCollisionEnter to work at it's best should i add an empty gameobject to the area that i want it to contact the enemy with and add a rigidbody and capsule ?or should i just add it to the armature of the character and add a rigidbody to one of its limb instead?The reason why i ask this questions is the fact that i have seen onCollisionEnter work best when it comes to projectiles instead with close combat(such as swords or fist).If anyone knows which way is best.Can you please tell me why?

Comment
Add comment · Show 6
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 skylem · Jan 21, 2015 at 12:34 AM 0
Share

It would help alot if you explained the use case of OnCollisionEnter, are you using this for some kind of in game attack/damage system ?

avatar image IKilledKenny_2 · Jan 21, 2015 at 01:52 AM 0
Share

Im Sorry,I really got to be more specific while asking questions....What i am trying to do is make it that when my character hits another character they character that got hit plays an animation depending on what animation that they got hti with.Ill put an example.

 #pragma strict
  var  enemy : Transform;
 
   
 function Start() {
  
         enemy = GameObject.FindWithTag("Dummy").transform;
 }
   
     
 
  function OnCollisionEnter(collision : Collision){
  if(collision.gameObject.tag == "Dummy") {
  
     if(!animation.IsPlaying("Boeton_Fighting_Stance"))
     if(!animation.IsPlaying("punch2"))
     if(!animation.IsPlaying("punch3"))
     if(!animation.IsPlaying("punch4"))
     if(!animation.IsPlaying("Sidekick_lightning"))
     if(!animation.IsPlaying("Falling_Axe_$$anonymous$$ick"))
     if(!animation.IsPlaying("Shadow_Spinning_Sidekick"))
     if(!animation.IsPlaying("Shadow_Calling"))
     if(!animation.IsPlaying("para_kick"))
     if(!animation.IsPlaying("Windmill"))
     if(!animation.IsPlaying("Shadow_Bandit_$$anonymous$$ick_3"))
     if(!animation.IsPlaying("One_Hand_Windmill"))
     if(!animation.IsPlaying("Rising_knee_Part_2"))
     if(!animation.IsPlaying("Jumping_Snap_$$anonymous$$ick"))
     if(!animation.IsPlaying("Triple_$$anonymous$$ick"))
     if(!animation.IsPlaying("Jumping_$$anonymous$$nee"))
     if(!animation.IsPlaying("Shoot_combo_2"))
     if(!animation.IsPlaying("Shoot_combo_2_2"))
     if(!animation.IsPlaying("Shoot_combo_2_3"))
     if(!animation.IsPlaying("Shoot_combo_2_4"))
     if(!animation.IsPlaying("Shoot_combo_2_5"))
     if(!animation.IsPlaying("Shoot_combo_2_3_001"))
     if(!animation.IsPlaying("$$anonymous$$azuya_pause"))
     if(!animation.IsPlaying("$$anonymous$$azuya_pause_2"))
     if(!animation.IsPlaying("Bullet_Barrage"))
     if(!animation.IsPlaying("Bullet_Barrage_Start"))
     if(!animation.IsPlaying("Bullet_Barrage_$$anonymous$$id"))
     if(!animation.IsPlaying("Star_Blitz"))
     if(!animation.IsPlaying("Fast_shot_3"))
     if(!animation.IsPlaying("Shoot1"))
     if(!animation.IsPlaying("Shoot2"))
     if(!animation.IsPlaying("Shoot3"))
     if(!animation.IsPlaying("Shoot4"))
     if(!animation.IsPlaying("Shoot5"))
     if(!animation.IsPlaying("540_Bullets_"))
  if(animation.IsPlaying("punch1")){
  
  enemy.animation.Play ("Hit_Stomach");
 
 
  
  
  
          }
      }
  }

avatar image skylem · Jan 21, 2015 at 05:01 AM 0
Share

in the same way you Get a gameobject from the collision component in your Collision you can actually use getcomponent to recieve any component that is upon the Other GameObject the way i would do this would be so that when i make contact with another gameobject i recieve its script for taking damage and then send a message to that script to apply it in this case it would play your animation for being hit, if you would like an example let me know and ill post it as an answer.

avatar image IKilledKenny_2 · Jan 21, 2015 at 07:42 AM 0
Share

Yes Please Sir! I am dying to understand this!

avatar image Salah Alddin · Jan 21, 2015 at 07:48 PM 0
Share

What was that does it make anything

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jimjames · Jan 21, 2015 at 04:43 AM

Try adding a empty parent object and see how it works out. Once you figure out the collision bugs and work them out, then add them to your limbs. If you are swinging a sword and you want the enemy to play an animation on hit, try using OnTriggerEnter instead of OnCollision. Ontrigger can still pass over damage amounts and alot more.

 void  OnTriggerEnter ( Collider other  )
     {
         if(other.gameObject.tag ==  "Dummy")
         {
             other.transform.gameObject.animation.Play ("Hit_Stomach"); //I think this is the right wording
         }
     }


Is all you need. No declaring variables needed. This will be attached to your "sword/fist" and will make the other gameobject play it's animation.

You can also turn on the collider at the beginning of the animation and turn it off at the end so you wont be dealing damage or/or animations to other objects while not swinging.

Hope this helped and what you are after.

James

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 · Jan 21, 2015 at 07:48 AM 0
Share

Yes it does answer my question but the last thing i need to know before i can fully understand OnCollisionEnter and OnTriggerEnter.How can i make it that when i hit an opponent with a certain animation the person that got hit with that animation does a certain animation?the reason is that i want my characters to grab each other and to do that they have to be able to do certain animations to each other I have a script for an example #pragma strict

 var setOnFire : ParticleSystem;
 var player :Transform;
 var speed = 30;
 var pushPower = 2.0;
 var anim: String;
 var cam: Camera;
 
 function OnTriggerEnter (Col : Collider)
  {
      if (Col.tag == "hitbox2")
      if (Col.tag == "Sword2")
      
      {
          // Default hit animation
          var anim : String = "Hit1";
          
          // Animation related to player's animation
          if(Col.animation.IsPlaying("punch2"))
           anim = "Hit_Stomach";
          
             
              anim = ("Hit_Stomach");
      
          animation.Play("Hit_Stomach");
          cam.animation.Play("HitAnimationCamera");
          cam.animation["HitAnimationCamera"].speed = 2;
          setOnFire.Play();
      }
  }
  
  function OnTriggerExit (other : Collider)
  {
          
          if (other.tag == "Player")
          {
          cam.animation.Play("Normal Camera");
 
          
          
          }
          
 }         
avatar image skylem · Jan 21, 2015 at 08:14 AM 0
Share

a sample of how to get a script component from the object u trigger. this the other gameobject will need a javascript named TakeDamage been a while since i've written javascript so let me know if it doesn't work and ill play with it.

 private var takeDamage : TakeDamage;
 
 function OnTriggerExit(other : Collider) {
 takeDamage = other.GetComponent("TakeDamage");
 }

then in the same manor you would fire a function from within a script u would use the following to make the other script do your custom function

 takeDamage.PlayAnim();

so upon the script TakeDamage you would require a function similar to the following.

 function PlayAnim() {
     animation.Play("Hit_Stomach");
 }


avatar image IKilledKenny_2 · Jan 21, 2015 at 06:31 PM 0
Share

Thanks So much i am going to try this right now =D

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

28 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

Related Questions

OnTriggerEnter() and OnCollisionEnter(), OnControllerColliderHit() not working with character controller 2 Answers

Movement Collisions for Flat NPC with Character Controller 0 Answers

OnCollision messages don't work with Character Controller? 1 Answer

Cannot move FPSController after respawning at a spawn point. 1 Answer

Collision CharacterController vs CharacterController 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