• 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
4
Question by HiddenMonk · Mar 16, 2016 at 10:04 PM · physicsdebugging visual

How can you visualize a BoxCast, BoxCheck, etc...

I would like to have the equivalent of a Debug.DrawRay for a BoxCast , BoxCheck, etc...

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

4 Replies

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

Answer by HiddenMonk · Mar 16, 2016 at 10:04 PM

With the help of this http://answers.unity3d.com/questions/461588/drawing-a-bounding-box-similar-to-box-collider.html I was able to make the below.

 using UnityEngine;
  
 public static class ExtDebug
 {
     //Draws just the box at where it is currently hitting.
     public static void DrawBoxCastOnHit(Vector3 origin, Vector3 halfExtents, Quaternion orientation, Vector3 direction, float hitInfoDistance, Color color)
     {
         origin = CastCenterOnCollision(origin, direction, hitInfoDistance);
         DrawBox(origin, halfExtents, orientation, color);
     }
     
     //Draws the full box from start of cast to its end distance. Can also pass in hitInfoDistance instead of full distance
     public static void DrawBoxCastBox(Vector3 origin, Vector3 halfExtents, Quaternion orientation, Vector3 direction, float distance, Color color)
     {
         direction.Normalize();
         Box bottomBox = new Box(origin, halfExtents, orientation);
         Box topBox = new Box(origin + (direction * distance), halfExtents, orientation);
             
         Debug.DrawLine(bottomBox.backBottomLeft, topBox.backBottomLeft,    color);
         Debug.DrawLine(bottomBox.backBottomRight, topBox.backBottomRight, color);
         Debug.DrawLine(bottomBox.backTopLeft, topBox.backTopLeft, color);
         Debug.DrawLine(bottomBox.backTopRight, topBox.backTopRight,    color);
         Debug.DrawLine(bottomBox.frontTopLeft, topBox.frontTopLeft,    color);
         Debug.DrawLine(bottomBox.frontTopRight, topBox.frontTopRight, color);
         Debug.DrawLine(bottomBox.frontBottomLeft, topBox.frontBottomLeft, color);
         Debug.DrawLine(bottomBox.frontBottomRight, topBox.frontBottomRight,    color);
     
         DrawBox(bottomBox, color);
         DrawBox(topBox, color);
     }
     
     public static void DrawBox(Vector3 origin, Vector3 halfExtents, Quaternion orientation, Color color)
     {
         DrawBox(new Box(origin, halfExtents, orientation), color);
     }
     public static void DrawBox(Box box, Color color)
     {
         Debug.DrawLine(box.frontTopLeft,     box.frontTopRight,    color);
         Debug.DrawLine(box.frontTopRight,     box.frontBottomRight, color);
         Debug.DrawLine(box.frontBottomRight, box.frontBottomLeft, color);
         Debug.DrawLine(box.frontBottomLeft,     box.frontTopLeft, color);
                                                  
         Debug.DrawLine(box.backTopLeft,         box.backTopRight, color);
         Debug.DrawLine(box.backTopRight,     box.backBottomRight, color);
         Debug.DrawLine(box.backBottomRight,     box.backBottomLeft, color);
         Debug.DrawLine(box.backBottomLeft,     box.backTopLeft, color);
                                                  
         Debug.DrawLine(box.frontTopLeft,     box.backTopLeft, color);
         Debug.DrawLine(box.frontTopRight,     box.backTopRight, color);
         Debug.DrawLine(box.frontBottomRight, box.backBottomRight, color);
         Debug.DrawLine(box.frontBottomLeft,     box.backBottomLeft, color);
     }
     
     public struct Box
     {
         public Vector3 localFrontTopLeft     {get; private set;}
         public Vector3 localFrontTopRight    {get; private set;}
         public Vector3 localFrontBottomLeft  {get; private set;}
         public Vector3 localFrontBottomRight {get; private set;}
         public Vector3 localBackTopLeft      {get {return -localFrontBottomRight;}}
         public Vector3 localBackTopRight     {get {return -localFrontBottomLeft;}}
         public Vector3 localBackBottomLeft   {get {return -localFrontTopRight;}}
         public Vector3 localBackBottomRight  {get {return -localFrontTopLeft;}}
 
         public Vector3 frontTopLeft     {get {return localFrontTopLeft + origin;}}
         public Vector3 frontTopRight    {get {return localFrontTopRight + origin;}}
         public Vector3 frontBottomLeft  {get {return localFrontBottomLeft + origin;}}
         public Vector3 frontBottomRight {get {return localFrontBottomRight + origin;}}
         public Vector3 backTopLeft      {get {return localBackTopLeft + origin;}}
         public Vector3 backTopRight     {get {return localBackTopRight + origin;}}
         public Vector3 backBottomLeft   {get {return localBackBottomLeft + origin;}}
         public Vector3 backBottomRight  {get {return localBackBottomRight + origin;}}
 
         public Vector3 origin {get; private set;}
 
         public Box(Vector3 origin, Vector3 halfExtents, Quaternion orientation) : this(origin, halfExtents)
         {
             Rotate(orientation);
         }
         public Box(Vector3 origin, Vector3 halfExtents)
         {
             this.localFrontTopLeft     = new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z);
             this.localFrontTopRight    = new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z);
             this.localFrontBottomLeft  = new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z);
             this.localFrontBottomRight = new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z);
 
             this.origin = origin;
         }
 
 
         public void Rotate(Quaternion orientation)
         {
             localFrontTopLeft     = RotatePointAroundPivot(localFrontTopLeft    , Vector3.zero, orientation);
             localFrontTopRight    = RotatePointAroundPivot(localFrontTopRight   , Vector3.zero, orientation);
             localFrontBottomLeft  = RotatePointAroundPivot(localFrontBottomLeft , Vector3.zero, orientation);
             localFrontBottomRight = RotatePointAroundPivot(localFrontBottomRight, Vector3.zero, orientation);
         }
     }
 
      //This should work for all cast types
     static Vector3 CastCenterOnCollision(Vector3 origin, Vector3 direction, float hitInfoDistance)
     {
         return origin + (direction.normalized * hitInfoDistance);
     }
     
     static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion rotation)
     {
         Vector3 direction = point - pivot;
         return pivot + rotation * direction;
     }
 }

Comment
Add comment · Show 11 · 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 LuLuKo · Jun 24, 2016 at 02:03 PM 0
Share

After a few tests.I think the value "halfExtents" in constructor "Box" should be divided by 2 will be more accurate.

 public Box(Vector3 origin, Vector3 halfExtents)
 {
    halfExtents /= 2; 
    this.localFrontTopLeft     = new Vector3(-halfExtents.x, halfExtents.y, -halfExtents.z);
    this.localFrontTopRight    = new Vector3(halfExtents.x, halfExtents.y, -halfExtents.z);
    this.localFrontBottomLeft  = new Vector3(-halfExtents.x, -halfExtents.y, -halfExtents.z);
    this.localFrontBottomRight = new Vector3(halfExtents.x, -halfExtents.y, -halfExtents.z);
  
    this.origin = origin;
 }


avatar image HiddenMonk LuLuKo · Jun 24, 2016 at 05:12 PM 0
Share

I designed the code in a way similar to the Physics.BoxCast method. To do a BoxCast, you must provide the already divided by 2 extents. It does not divide for you. So too for the BoxCast visualizer code above, it is expecting you to provide the already divided by 2 extents. Thats why its named "halfExtents", it wants the half extents.

For example...

 Vector3 origin = transform.position
 Vector3 halfExtents = Vector3.One / 2f;
 Vector3 direction = transform.forward;
 Quaternion orientation = transform.rotation;
 float distance = 10f;
 
 RaycastHit hitInfo;
 if(Physics.BoxCast(origin, halfExtents, direction, out hitInfo, orientation, distance))
 {
     ExtDebug.DrawBoxCastOnHit(origin, halfExtents, orientation, direction, hitInfo.distance, Color.red)
 }

If this confusion was not the issue and there is actually something wrong with the code, please let me know and I will look into it more =)

Also, I should probably swap the orientation and direction parameter order in the visualizer code so that it is even more like the Physics.BoxCast parameter order.

avatar image LuLuKo HiddenMonk · Jun 25, 2016 at 06:34 AM 1
Share

Ah...you're right, and I think I know what make me confuse.

I was using Physic2D.BoxCast for testing, and I thought it worked the same way as 3D BoxCast, but I was wrong, 2D BoxCast second value is "size", and it's no need to divide into 2.

So I decided to add a method "DrawBoxCast2D" in the code you provided so that I won't feel strange every time I use it. :P

avatar image canis · Feb 25, 2017 at 09:04 AM 0
Share

thanks for the code! super helpful !!

avatar image siddharth3322 · Jan 06, 2018 at 10:20 AM 0
Share

Can you provide method that work with Physics2D.BoxCast ??

avatar image LuLuKo siddharth3322 · Jan 07, 2018 at 04:05 AM 1
Share

here:

 public static void DrawBoxCast2D(Vector2 origin, Vector2 size, float angle, Vector2 direction, float distance, Color color)
 {
         Quaternion angle_z = Quaternion.Euler(0f, 0f, angle);
             DrawBoxCast(origin, size / 2f, direction, angle_z, distance, color);
 }

avatar image siddharth3322 LuLuKo · Jan 10, 2018 at 02:54 PM 1
Share

Thank you, it worked perfectly :)

Show more comments
avatar image Brankov · Aug 29, 2018 at 02:05 PM 0
Share

This was amazingly helpful to discern why my BoxCast does not work! Thank you.

avatar image sayginkarahan · Mar 10, 2021 at 10:42 PM 0
Share

Thank you, it worked amazing!

avatar image
1

Answer by felix11 · Jun 26, 2016 at 07:19 AM

What about Gizmos? Gizmos.DrawCube();

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 HiddenMonk · Jun 26, 2016 at 07:30 AM 1
Share

I preferred a method similar to Debug.DrawRay so that I could put it anywhere in my code and not rely on OnDrawGizmos.

avatar image
0

Answer by WarOnGravity · Nov 24, 2018 at 11:57 AM

Can anyone help me understand how to use this code? I don't understand where it goes.

I managed to get boxcasts reporting correctly, but my Gizmos.DrawWireCube(), do not rotate properly. I'd love to be able to see them. Thanks!

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 SgtOkiDoki · Nov 26, 2018 at 05:15 PM 0
Share

'my Gizmos.DrawWireCube(), do not rotate properly'

Use Gizmos.matrix ^^

avatar image
0

Answer by ModernArtery · Oct 22, 2020 at 04:48 AM

Really helpful code, thanks! I'm currently trying to get hitboxes figured out, and having an effective visual reference like this is fantastically helpful.

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

66 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

Related Questions

2D 360 degress platformer example needed 0 Answers

Script doesn't work with framerate drop 1 Answer

Constant force, with gravity, and correct collisions. 2 Answers

AI Turret Aiming 2 Answers

Physics and gravity of my 3D model 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