Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kylecat6330 · Feb 03, 2020 at 07:30 PM · collisionraycast3ddots

How to determine which side of an object the player collided with?

I have been using a combination of ray casts coming from the player and dots to tell which side of a cube the player is against. I do not need how much to the right or how far forward the player is all I need is whether they are on the right, left, front, or back side of the cube. I've been using this code and had been working pretty well.

 wall = hit.transform;
 Vector3 hitLoc = wall.InverseTransformPoint(hit.point);
 Vector3 hitDir = hitLoc.normalized;
 
 dotF = Vector3.Dot(wall.TransformDirection(Vector3.forward).normalized, hitDir);
 dotR = Vector3.Dot(wall.TransformDirection(Vector3.right).normalized, hitDir);
 if (Mathf.Abs(dotF) > Mathf.Abs(dotR))
 {
      dotR = 0.0f;
      if (dotF > 0)
      {
            dotF = 1.0f;
      }
      else if (dotF < 0)
      {
           dotF = -1.0f;
      }
 }
 if (Mathf.Abs(dotR) > Mathf.Abs(dotF))
 {
      dotF = 0.0f;
      if (dotR > 0)
      {
           dotR = 1.0f;
      }
      else if (dotR < 0)
      {
           dotR = -1.0f;
      }
 }


However once I started using cubes that were rotated I started having problems. Anytime the player nears the edges of the cubes it will switch to say that it is on another side.

Example: If the player is on the front side of a cube that is rotated 165 on the y axis and moves toward the edge then it suddenly says the player is on the right side.

I'm no expert when it comes to this kind of stuff, and can't figure out how to make it work. Maybe dots were the wrong way to go. As long as I can tell which side of the cube the player is against I don't care what method is used, so any suggestions are welcome. Thanks.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by unity_ek98vnTRplGj8Q · Feb 04, 2020 at 04:28 AM

Maybe there is a better way to do this, but my first instinct would also be to use the dot product. I do notice a couple big mistakes in your code however. Don't use InverseTransformDirection on hit.point. If you want to get the direction of the hit from the center of the object use hit.point - hit.transform.position. My code would look something like this (I have not tested this code)

 Vector3 hitDirection = hit.point - hit.transform.position;
 
 //Dot products
 float upWeight = Vector3.Dot(hitDirection, hit.transform.up);
 float forwardWeight = Vector3.Dot(hitDirection, hit.transform.forward);
 float rightWeight = Vector3.Dot(hitDirection, hit.transform.right);
 
 //We care about the absolute value only for now
 float upMag = Mathf.Abs(upWeight);
 float forwardMag = Mathf.Abs(forwardWeight);
 float rightMag = Mathf.Abs(rightWeight);
 
 if(upMag >= forwardMag && upMag >= rightMag){
     if(upWeight > 0) //Up
     else //Down
 }
 else if(forwardMag >= upMag && forwardMag >= rightMag){
     if(forwardWeight > 0) //Forward
     else //Back
 }
 else{
     if(rightWeight > 0) //Right
     else //Left
 }
Comment
Add comment · Show 1 · 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 kylecat6330 · Feb 04, 2020 at 07:48 PM 0
Share

Thanks for the help. That wasn't the problem, but I'm sure it would have become one later.

If anyone is wondering the problem was just that I had four ray casts one for each direction going out from the player. Whenever the player was at an angle two of them would touch the cube at once, so whenever the player approached the edge one would go off the edge and hit something else. It didn't always hit something else which was why the problem was so inconsistent.

avatar image
0

Answer by haseeb5639 · Feb 04, 2020 at 05:25 AM

Use four colliders on 4 different sides. Give them 4 different tags. SIMPLE!

Comment
Add comment · Show 1 · 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 kylecat6330 · Feb 04, 2020 at 04:29 PM 0
Share

I mean you're technically right that would easily solve my problem. However, it might be a bit of a hassle to work with multiple colliders every time I have to make a cube or change the dimensions of one. Thanks for the idea though and it'll be good to have in case I can't find a solution that works with just a single cube.

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

249 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 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 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 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 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 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 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 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 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

Cylinder Ground Collision 0 Answers

Problem with using Ray Casting to avoid obstacles and walls.. 1 Answer

Adding a raycast to prevent camera from going through walls? 0 Answers

How to add platform motion to player controller without parenting? - Mostly working code 1 Answer

Raycast for 2d with touch. 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