• 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 I_Am_Gamer · Apr 11, 2015 at 06:16 PM · raycastdirectionspherecollider

Detect from what direction a shere collider was hit

I've read a few posts on a similar question but most seem to talk about box colliders and sides. in my case the collider would be a sphere.

Would using raycast tell me what direction the hit occurred on the sphere collider. is this even possible?

Comment
Add comment · Show 1
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 fafase · Apr 11, 2015 at 06:23 PM 0
Share

It is always the same, get the hit point from the collision and get the normalized vector between the sphere center and the hit point.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Killhatch · Apr 11, 2015 at 07:11 PM

The collision object you are passed in OnCollisionEnter contains a field called Contacts. A ContactPoint class has a Point field (of type Vector3). To find the incoming direction of the hit, subtract the contact point's position from the that of the colliding object.

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 _Gkxd · Apr 11, 2015 at 08:34 PM 0
Share

Every contact point contains a normal vector, which already gives you the direction from which an object is hit, so there is no need to subtract two positions. (Also, the position of a collider isn't necessarily its center for some objects, but that's not something most people have to worry about.)

RaycastHit has a similar normal vector too.

avatar image Killhatch · Apr 11, 2015 at 09:18 PM 0
Share

That is incorrect. The normal field contains the normal vector of the triangle that was hit.alt text

I would not call that "the direction from which an object is hit". The RaycastHit returns the same thing.

We can agree that the position of the collider isn't necessarily accurate but as you say yourself it is not something most people have to worry about.

contactnormal.png (105.1 kB)
avatar image _Gkxd · Apr 12, 2015 at 04:43 AM 0
Share

I guess it's just a difference between how we define what "direction hit" is. When I look at that picture, the sphere is being hit in the direction of the red arrow.

In this case, "direction hit" can also be defined as the velocity that the black line is travelling in right before it hits the sphere. It really depends on context and what effect you want to create.

avatar image Killhatch · Apr 12, 2015 at 09:52 AM 0
Share

OP has not specified it clearly but it might very well be what you mean. We will have to ask for an elaboration.

avatar image
0

Answer by lordlycastle · Apr 11, 2015 at 09:28 PM

You can find the direction the sphere was hit from, by finding out the direction of the velocity of the projectile that hit it. Simply implement OnCollisionEnter method in the collider that is going to get hit, and find the velocity of the colliding object using it’s rigid body. Remember you’ll have to inverse that vector to get the absolute direction, but that depends on what you plan on doing with it.

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 I_Am_Gamer · Apr 13, 2015 at 12:24 PM 0
Share

Hopefully this will help any confusion due to my description lol.

alt text

Green arrow is the player object and its direction. Circle is the sphere which is child object. Red arrow points to the position at which the sphere was hit.

alt text

I need to know the position the hit occurred so I can instantiate something at (near to the sphere) in that direction.

spherecollision2.png (2.4 kB)
spherecollision.png (2.5 kB)
avatar image lordlycastle · Apr 18, 2015 at 01:59 AM 1
Share

@I_Am_Gamer The Collision class has a variable Contacts, that contains all the contact points. Read up the scripting API. It also has relative velocity, and you cal always find the absolute velocity if you need it.

 void OnCollisionEnter(Collision collision) {
     ContactPoint contact = collision.contacts[0];
     Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
     Vector3 pos = contact.point;
     Instantiate(explosionPrefab, pos, rot) as Transform;
     Destroy(gameObject);
 }


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

19 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

Related Questions

How can I raycast the direction my 2D character is facing? 1 Answer

Making My Raycast Face The Right Direction 3 Answers

Raycast between TransformDirection and Vector3.Forward? 1 Answer

How to shoot raycast with slight random direction? 1 Answer

Raycast direction problem 3 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