- Home /
The question is answered, right answer was accepted
Problems with Physics.Boxcast ( reward for fixing 10€ via paypal )
So i created this test scene to show you my problem
i wanted to detect how far my cube( for this example ) can move in a certain direction. I use Physics.Boxcast instead of raycast because raycast is way to thin.
I used Physics.Spherecast in past and it worked perfectly fine but i wanted to use Boxcast because my Object isn't a sphere
so my problem is that i get completly wrong hit.distance values and also my gizmos always show a distance betweet ground and itself for reasons i dont understand.
I want my Boxcast to go all the way down to the ground or the wall and give me the right hit.distance value
If you can fix my problem you can send me a email with your paypal data after and i give you a small donation, because you really don't find much about this on the web.
and a screenshot of my scene
using System.Collections; using System.Collections.Generic; using UnityEngine; public class testscript : MonoBehaviour { public float maxDistance; public LayerMask layerMask; private Vector3 origin; private Vector3 direction; private float currentHitdistance; void Update () { origin = transform.position; direction = Vector3.down; RaycastHit hit; if (Physics.BoxCast(origin,new Vector3(1,1,1),direction,out hit, Quaternion.LookRotation(direction),maxDistance,layerMask,QueryTriggerInteraction.UseGlobal)) { currentHitdistance = hit.distance; Debug.Log(hit.distance); } } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireCube(origin + direction * currentHitdistance, new Vector3(1,1,1)); } }
I don't care about the donation or whatever, but I am curious, what do you mean it is giving improper distance? You are aware that it gives the distance traveled, correct? Which means your box having halfextents of 1, makes the box size 2, and as such, the box will never calculate the distance to the center of the box, but the first edge of the box that touches the terrain. So automatically this will be a $$anonymous$$imum of 1 meter away.
thank you i didnt knew this
also did it just delete my first reply, i am knew at this forum
fuck i didnt knew this thank you i feel like the biggest idiot right now guess i just have to add half the rest amount then, thank you
Answer by taylank · Mar 26, 2018 at 05:12 PM
When you use Vector3(1,1,1) with BoxCast, that's the half extents of your box, so your real box size is assumed to be 2,2,2. Whereas with DrawWireCube the argument you pass is size, which is the whole length of an edge. So you are visualizing two mismatched lengths.