• 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 DrZoop · Jun 08, 2016 at 04:14 PM · raycastraycasthitboxcollider

Boxcast returns one incorrect hit point of (0,0,0).

I have a box collider, and to test if it is near the ground, I use a box cast:

 RaycastHit[] raycastHits = Physics.BoxCastAll(boxCollider.gameObject.transform.TransformPoint(boxCollider.center), boxCollider.size * 0.5f, Vector3.down, boxCollider.gameObject.transform.rotation, 0.3f);

Originally I was using BoxCastNonAlloc, but I switched to the above to debug, because I feared it was something weird with the NonAlloc functionality. They should be the effectively the same, and they did return the same results:

 int hits = Physics.BoxCastNonAlloc(boxCollider.gameObject.transform.TransformPoint(boxCollider.center), boxCollider.size * 0.5f, Vector3.down, raycastHits, boxCollider.gameObject.transform.rotation, 0.3f);

I have confirmed that that the cast is going where I expect it to go with some debug logging and draw lines.

It is correctly registering hits, however, it seems that the first hit always returns an incorrect point: (0, 0, 0). The normal usually seems to be correct, but hard to confirm given that it's being called every update. So, essentially, if I hit anything, I always get at least one RaycastHit where the point is (0, 0, 0), and the normal is (0, 1, 0), presumably because it is hitting the floor.

Example outputs of hits, in point then normal order:

One incorrect, two correct:

 3: (0.0, 6.0, -21.5) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0), (0.0, 6.2, -21.4) (0.0, 1.0, 0.0), 

One incorrect, one correct:

 2: (-0.7, 6.0, -21.1) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0), 

All of them incorrect:

 2: (0.0, 0.0, 0.0) (0.0, 1.0, 0.0), (0.0, 0.0, 0.0) (0.0, 1.0, 0.0),

It is correct in the number of hits, just the data that is returned is incorrect at least one time in every array of results returned.

Just for additional information, here is the code used to print out the hits. "hits" is equal to the length of the array returned for BoxcastAll, or equal to the value returned from BoxCastNonAlloc:

 string message = hits.ToString() + ": ";
 for (int i = 0; i < hits; ++i)
 {
     message += raycastHits[i].point + " " + raycastHits[i].normal + ", ";
     Debug.DrawRay(raycastHits[i].point, raycastHits[i].normal, Color.red, 1f);
 }
 Debug.Log(message);

I am using the latest beta of Unity 5.4, beta 20. I checked the history there and didn't see anything regarding incorrect RaycastHit results with Boxcast.

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 Distant-Object · Jun 01, 2017 at 11:29 PM 0
Share

I too have stumbled into this problem. Pretty much a year later after you posted this. Hopefully this is addressed soon!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by McGravity · Jun 18, 2017 at 07:55 AM

There is a note under the entry in the documentation which I guess explains the behaviour:

https://docs.unity3d.com/ScriptReference/Physics.BoxCastAll.html

... and the zero vector gets returned in RaycastHit.point. ...

Your BoxCast hits the BoxCollider of your Player at the start of the sweep.

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.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast is not returning exact number / Mesh is not exact dimensions 2 Answers

Mesh polygon of a hit surface on a gameobject 1 Answer

Detect when object is no longer hit by Raycast 2 Answers

How to place an object in the direction of a raycast 1 Answer

ray cast returning the wrong object, going through multiple objects 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