- Home /
Sweep test
Hi,
I am trying to find all objects that may be in the path of my body. I'm not sure I am using sweep test correctly. As my body rotates (it's a child) I don't think the sweep is rotating correctly with the child.
Debug.DrawLine (p.bodyhit.transform.position, g1, Color.red);
if (p.bodyhit.SweepTest(p.bodyhit.transform.forward, out hit, 1))
{
if (hit.collider.gameObject.tag != "plane")
{
print("ray:"+hit.collider);
move_ok=false;
}
}
Any help appreciated (also I can't get the debug drawling to do as I would expect).
thanks
Answer by aldonaletto · Sep 21, 2011 at 12:49 AM
Just to make things clearer, what's p.bodyhit? The object's rigidbody? It should be, since SweepTest is a rigidbody function.
Anyway, I suspect you're using a distance too short to hit something. I changed the script a little to include a range variable, which sets the max distance. I've assumed here that p is the object and bodyhit is its rigidbody.
var range: float = 10; Debug.DrawLine (p.transform.position, p.transform.forward*range, Color.red);
if (p.bodyhit.SweepTest(p.bodyhit.transform.forward, out hit, range)) { if (hit.collider.tag != "plane") { print("ray: "+hit.collider.tag); // print the object's tag (change to name, if you prefer) move_ok=false; } }
Thanks!, that works. Though I notice sweep test doesn't seem particularly accurate. I can be ai$$anonymous$$g directly at a body and the drawn line flickers on/off ; as it 'sometimes' doesn't find a body even though nothing is moving. Also, while firing the sweep test across the corner of an object nothing is found; again visually it should be and also once more everything is stationary. Why is it so unstable?
thanks