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

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;
    }
}