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
2
Question by johnmph · Apr 11, 2014 at 12:07 AM · raycastingspherecastaccuracyreflect

Physics.SphereCast with Vector3.Reflect returns weird results

Hello, I try to have a ray that simulates the position of a ball which will move and collide with others objects. I use the Physics.SphereCast and Vertex3.Reflect method to do this but I got bad results, I added a screenshot, in red it's the calculated ray and in green as it should be.

Screenshot

Here is my script to show the ray :

 // Create start ray
 Ray ray = new Ray(objectWorldPosition, direction);
 
 // Ray loop
 RaycastHit hitInfo;
 for (float length = 0.0f; length < 100.0f; length += hitInfo.distance) {
     // If raycast hits something
     if (Physics.SphereCast(ray, 0.5f, out hitInfo)) {
         // Calculate center position of ball
         Vector3 centerVector = hitInfo.point + (hitInfo.normal * 0.5f);
 
         // Draw line
         Debug.DrawLine(ray.origin, centerVector, Color.red);
 
         // Get vector from start to hit point
         Vector3 rayVector = centerVector - ray.origin;
 
         // Calculate reflective ray vector
         Vector3 reflectiveRayVector = Vector3.Reflect(rayVector, hitInfo.normal);
 
         // Update ray
         ray = new Ray(centerVector, reflectiveRayVector);
     } else {
         // Exit loop
         break;
     }
 }
 

The green ray is calculated with a Physics.RayCast and a Pythagore formula to find the center point of the ball from the hitpoint. It is more accuracy but with some angles that fails too and also on some situations where ball could not pass but ray does because it is thinner.

Can someone help me ? Thanks

capture d’écran 2014-04-10 à 22.58.57.png (55.6 kB)
Comment
Add comment · Show 2
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 ToxxicSin · Apr 11, 2014 at 03:32 AM 0
Share

Well it depends. Because of the size of the ball, the side of the ball will hit the wall, where as the ray simulating the center of the ball isn't hitting? Just a guess. Because the ray simulates the center of the ball where as the ball itself hits the wall and not the center of it. Could also be because the ball is rotating or has other physics affecting its path. $$anonymous$$ake sure you have the radius set right too

avatar image johnmph · Apr 11, 2014 at 06:20 AM 0
Share

With SphereCast, it finds the hitpoint for the side of the ball, I convert then the hitpoint to the position of the center by adding the normal vector of the collider multiplied by the radius of the ball. The radius is correct and the ball is constrained in x, y and z for rotation.

1 Reply

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

Answer by johnmph · Apr 14, 2014 at 04:17 PM

It is because Physics.SphereCast returns a wrong normal vector.

See http://answers.unity3d.com/questions/50846/how-do-i-obtain-the-surface-normal-for-a-point-on.html

Comment
Add comment · 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

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

22 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

Related Questions

Manuals Sphere collison 0 Answers

Raycast Never Hits Anything?! 2 Answers

SphereCast inaccurate? 0 Answers

Raycast // Accuracy math pls help 1 Answer

How to identify multiple objects in a sphere cast? 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