• 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
1
Question by dissidently · Dec 17, 2010 at 07:57 AM · raycastboxcollider

box colliders do face detection?

If a raycast hits a box collider, does it return which face of the box it hit? If so, how do I get this information? If a raycast hits a box collider and returns which face it hit, does it return WHERE on the face it hit? If so, how do I get this information?

For a missile, aimed at an object with a box collider on it, what is the best method of determining IF the missile has hit, and WHERE on the box collider it's hit? Is it sending a raycast and somehow timing it with the arrival of the missile at the target, or using a collider on the missile and the target?

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

Answer by Eric5h5 · Dec 17, 2010 at 08:16 AM

Assuming "hit" is a RaycastHit, you can use hit.transform.InverseTransformPoint(hit.point) to get the local position of the raycast hit. From that you can determine which face was hit and where. Actual mesh information is not available unless you use a mesh collider.

If you want to do something like launch an object from whichever face of the cube was hit, all you need is info from the RaycastHit, since you don't actually care what face was hit per se, and you don't need to use InverseTransformPoint:

// Instantiate a prefab at the center of the cube,
// and orient it by the normal of the hit point
var clone = Instantiate(prefab,
                        hit.transform.position,
                        Quaternion.FromToRotation(Vector3.forward, hit.normal));
// Move it forward along its z axis by a certain amount
clone.transform.Translate(Vector3.forward * distanceFromCenter);
// Get it moving in its forward direction
clone.rigidbody.AddRelativeForce(Vector3.forward * forceAmount);

Where distanceFromCenter and forceAmount would be defined as public variables like this:

var distanceFromCenter = 2.0;
var forceAmount = 50.0;

For a missile, use OnCollisionEnter (other : Collision), where "other" contains Collision.contacts, which contains a ContactPoint array of the collision.

Comment
Add comment · Show 14 · 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 Proclyon · Dec 17, 2010 at 08:51 AM 0
Share

Does this take the rotation of the target object into consideration aswell?

avatar image Eric5h5 · Dec 17, 2010 at 09:51 AM 0
Share

Yes, it would be mostly useless if it didn't.

avatar image dissidently · Dec 17, 2010 at 01:44 PM 0
Share

O$$anonymous$$. I see how InverseTransformPoint converts a world space point to a local point, but how does that help me deter$$anonymous$$e which face was hit if the target Box collider has been rotated under physics/randomness?

Let me try to answer my own question, and tell me where I'm wrong.

Assu$$anonymous$$g the Box collider has been rotated 90 degrees counter clockwise around it's Local Y, and then 90 degrees clockwise around it's local X, the face that was facing the camera at game start is now facing downwards, and what was the left facing face at start is now facing the camera, 90 degrees clockwise rotated.

avatar image dissidently · Dec 17, 2010 at 01:46 PM 0
Share

so assu$$anonymous$$g I've fired from the camera point of view, and it's hit the current front facing face, which is actually the boxes original Left face, to deter$$anonymous$$e this, I've got to somehow compare the local rotation of the object with the box collision's center to the spot where it got hit?

avatar image Jesse Anders · Dec 17, 2010 at 04:06 PM 0
Share

If you've transformed the point into the local space of the object, you don't have to worry about the object's rotation. In the object's own local space, the box is axis-aligned and centered where specified (typically at the origin, unless you've changed it). As such, deter$$anonymous$$ing which face has been intersected can easily be deter$$anonymous$$ed by exa$$anonymous$$ing the local-space coordinates of the intersection point.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Need help with Raycasting 1 Answer

FPS rigidbody problem 0 Answers

Why does my Raycast stop detecting colliders 2 Answers

Raycasting Collision for player movement in a maze 3 Answers

BoxCollider2D is not working as expected 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