- Home /
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:
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!
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));
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.
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
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?
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
![](https://koobas.hobune.stream/wayback/20220612181940im_/https://answers.unity.com/themes/thub/images/avi.jpg)