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 PrimeDerektive · May 24, 2010 at 04:22 PM · colliderprojectileignore

How to do a frontal-only shield of sorts?

I'm trying to figure out the best way to implement a shield for a 3rd person melee combat game. Basically what I want to do is make it so that when I hold down the right mouse button, all attacks that hit my player in the front do no damage. My melee system is very simple, the characters right now are just shooting projectiles that disappear after a few feet.

I tried having a box collider parented to my player, positioned at the front, and activate/deactivating it on Fire2, but if I get too close to my enemies, their attacks actually go through the box collider because they're clipping through it.

Is there a way I could calculate the position of the collision of the melee projectiles when they hit my character, and determine if they are on the front of him?

UPDATE [SOLVED]:

Awesome! Thanks to equalsequals,I got it figured out with the Vector3.Dot. The vector math is baffling to me, but by constantly printing the dot product of the melee projectile's forward, and the forward of the player (the object the collision is happening on), I figured out that if the dot product is -1, the projectile and the player are facing each other. Here's the jist of the final code:

function OnCollisionEnter(other : Collision){

     var forward = transform.TransformDirection(Vector3.forward);
     var otherForward = other.transform.TransformDirection(Vector3.forward);
     var dotProduct = Vector3.Dot(forward,otherForward);

     if(dotProduct < -0.9){
         print("in the face!");
     }
     else{
         print("not in the face!");
     }

}

I went with the range of -0.9 - -1.0 to give it some leeway. Thanks again Equalsequals!

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
2
Best Answer

Answer by equalsequals · May 24, 2010 at 05:15 PM

You could try raycasting and if it intersects the collider they cause no damage.

[Edit]

If the raycast method doesn't work for whatever reason, you could try using some sort of comparison of the 2 melee-fighters Transform.forward. Perhaps a Dot Product (Vector3.Dot) between the 2 , if it is less than 0 I believe then they would be facing each other. Combine that with an arbitrary thresh hold and that could work for detecting whether an angle of approach should be blocked or not.

Don't quote me on this code, as it is just a quick scratch to get you started:

function OnCollisionEnter(other:Collision):void{ var otherV3:Vector3 = other.collider.gameObject.transform.position;

if(Vector3.Dot(transform.position,otherV3) < 0) { print("facing"); }}

Hope that helps.

==

Comment
Add comment · Show 4 · 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 PrimeDerektive · May 24, 2010 at 06:08 PM 0
Share

Wouldn't I run in to the same problem with raycasting though? No matter how I set up the box collider, if the enemy gets too close he clips through it (even though he shouldnt be able to), so the "melee projectiles" hit my player anyway

avatar image equalsequals · May 24, 2010 at 06:47 PM 0
Share

I suppose that could be a case. I'll revise my answer with another idea I have.

avatar image PrimeDerektive · May 24, 2010 at 08:36 PM 0
Share

Awesome! Check my update.

avatar image equalsequals · May 24, 2010 at 08:42 PM 0
Share

Glad I could help out!

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

No one has followed this question yet.

Related Questions

Network - Projectile, Prevent Collision with Parent 2 Answers

Projectiles without colliders or rigid bodies? 1 Answer

Special Collider? 1 Answer

object with multiple colliders ignoring mousedown for some. 3 Answers

Ignore Collider 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