Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 WarOnGravity · Nov 11, 2018 at 04:14 PM · gizmoscastcollider.raycast

Boxcast/Raycast Orientation

I started with the API Scripting reference's Physics.BoxCast example code: [https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html] ...and it works great... until the game object rotates, see: alt text

I know I've been something crazy for other people to try to look at, but here are some snippits of one ray/boxcast:

         public float w = 9f;
         public float h = 34.5f;
         public float d = 24;
         bool m_HitDetectFORE;
         RaycastHit m_HitFORE;
         float meausure_MaxDistance = 144f;
     
     void FixedUpdate() {
             
             m_HitDetectFORE = Physics.BoxCast((transform.position + new Vector3(w / 2, h / 2, -d)), new Vector3(w / 2, h / 2, 1f), -transform.forward, out m_HitFORE, transform.rotation, meausure_MaxDistance);
             if (m_HitDetectFORE) { Debug.Log("FORE Hit : " + m_HitFORE.collider.name); }
     }
     
     void OnDrawGizmos() {
             Gizmos.color = Color.cyan;
     if (m_HitDetectFORE) {
                      Gizmos.DrawRay((transform.position + new Vector3(w / 2, h / 2, -d)), -transform.forward * m_HitFORE.distance);
                 Gizmos.DrawWireCube((transform.position + new Vector3(w / 2, h / 2, -d)) + -transform.forward * m_HitFORE.distance, new Vector3(w, h, 1f));
             } else {
                      Gizmos.DrawRay((transform.position + new Vector3(w / 2, h / 2, -d)), -transform.forward * meausure_MaxDistance);
                 Gizmos.DrawWireCube((transform.position + new Vector3(w / 2, h / 2, -d)) + -transform.forward * meausure_MaxDistance, new Vector3(w, h, 1f));
             }

I have benefited enormously from reading the wealth of existing answers on this forum, but I can't piece together what's going on with this contraption. Thanks!

untitled.png (79.1 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 WarOnGravity · Nov 11, 2018 at 08:53 PM 0
Share

Wouldn't this mean that the cast starts at an offset from the parent's origin, then always faces forward, after the parent turns? Why wouldn't the origin move?

 Gizmos.DrawWireCube((transform.position + new Vector3(w / 2, h / 2, 0)) + transform.forward * m_HitSTRN.distance, new Vector3(w, h, 1f));
avatar image WarOnGravity · Nov 24, 2018 at 12:35 PM 0
Share

This is my latest attempt... still no luck.

     m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, new Vector3(transform.localScale.x / 2, transform.localScale.y / 2, 0.5f), transform.forward,   out m_Hit, Quaternion.LookRotation(transform.forward), m_$$anonymous$$axDistance);

Any help would be appreciated.

2 Replies

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

Answer by hexagonius · Nov 24, 2018 at 01:56 PM

Adding a simple Vector to the transform.position will always add a offset in world space, no matter the orientation of your cube. If you want let's say an offset into the cubes forward direction, use transfrom.position + transform.forward * offset

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
avatar image
0

Answer by WarOnGravity · Nov 24, 2018 at 07:30 PM

Thanks @hexagonius , I ended up just keeping everything pivoting off the center, and adjusting the distance. It didn't want to read "0" distance, so I made it report "Boxcast distance from center minus half the geometry dimensions."

How do I orientate the wirecube gizmos?

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 hexagonius · Nov 24, 2018 at 09:37 PM 0
Share

you can't as there is just the version with position and size.
you can use Graphics.Draw$$anonymous$$esh to achieve this, but besides a lot more parameters you also need a wireframe shader/ material

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

94 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

Related Questions

add a float and object 1 Answer

float to string c# unity script Android csharp 2 Answers

how to hit a gui button with raycast 2 Answers

Physic Collider Test Rotation 0 Answers

Upcasting via GetComponent<>() 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