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 AlejandroGorgal · Dec 03, 2013 at 12:16 AM · rigidbodycollidertrigger

Is it possible to block a collider with a trigger from hitting and object behind it?

Hi, Im working on a melee combat system which is currently setup this way:

The weapon has a RigidBody (set to kinematic and with gravity off) and a capsule collider (set as a trigger), when the weapon's collider hits the enemy it sends a message to apply damage. This is working great for me and it's most likely the way I will go about this since it's the most realistic and precise melee method I could find.

Now Im trying to develop a shield that will block these attacks but I can't seem to figure out the best way to do it. The idea is that if the weapon's collider hits the shield it wont do any damage to the character holding it, but the problem is that since this collider is a trigger it goes right trough the shield and hits the character anyways.

I realize I can probably use "Physics.IgnoreCollision" or simply disable the enemy's collider/rigidbody so that when he's holding the shield it doesnt do any damage, but this is not really what Im looking for, I want enemies to be able to block incoming attacks and still take damage if they are hit on the back (or anywhere other than the shield).

One possible solution Im thinking of is implementing a "bounce back" animation, so that if you hit a shield you'll immediately bounce back thus preventing the weapon from ever hitting the enemy, but Im not sure how to call an animation on a collision event or if that would even work at all.

Here's the code in charge of sending the damage notification:

 var player : GameObject;
 
 var AttackDamage : float = 25.0;
 
 function Start () 
 {
     Physics.IgnoreCollision(collider, player.collider);
 } 
  
 function OnTriggerEnter(other : Collider)
 {
     if(other.GetComponent(EnemyAI))
     {
         other.gameObject.SendMessageUpwards("ApplyDammage", AttackDamage, SendMessageOptions.DontRequireReceiver);
     }
 }
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
1
Best Answer

Answer by Spinnernicholas · Dec 03, 2013 at 01:06 AM

I would store a blocked flag on the sword that will stay true as long as it is colliding with a shield. Then, when it hits another part of the character, it will know that it is block and you can prevent it from sending a damage message.

 bool blocked = false;
 GameObject blockedBy;
 //.........
 void OnTriggerEnter(Collider other)
 {
   if(other.GetComponent(Shield)
   {
     blocked = true;
     blockedBy = other.gameObject;
   }
   else if(other.GetComponent(EnemyAI) && !blocked)
   {
     other.sendMessage("ApplyDammage", AttackDamage, SendMessageOptions.DontRequireReceiver);
   }
 }
 
 void OnTriggerExit(Collider other)
 {
   if(other.gameObject == blockedBy)
   {
     blocked = false;
     blockedBy = null;
   }
 }
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 AlejandroGorgal · Dec 03, 2013 at 01:31 AM 0
Share

Awesome! Im going to have to tweak it a bit since it seems slightly unreliable but the basics for the implementation do seem to work so I think this will be the way to go.

Thank you kindly for your answer, it helped me out a great deal :)

avatar image Spinnernicholas · Dec 03, 2013 at 05:42 AM 0
Share

I wrote it on the fly, without testing it, so it will probably need lots of tweaking. I'm glad I could help you in the right direction. just a note this is a re-purposing of a script to detect when I character in a platformer is grounded.

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

17 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

Related Questions

Objects placed manually collide, but don't if instantiated? 1 Answer

OnTriggerEnter not being called, have trigger, RB, and colliders set? 1 Answer

Trigger via Collider 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Do continuous and continuous dynamic collision detection work with trigger colliders? 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