- Home /
using Raycast to detect collision
So i have an object im wanting to move till it reaches the object. I tried checking if the coordinates match but that didnt work. So i decided to use a raycast since the moving object is moving in one direction the whole time. The only problem im running into is that its telling me their colliding when they are no where near close. please help.
RaycastHit hit = new RaycastHit();
Ray ray = new Ray(smokeMover.transform.position, -curVec.normalized);
if (!Physics.Raycast(ray,out hit,1,-1) && hit.transform != nextRoot.gameObject.transform)
{
Debug.Log("NO COLLISON");
Debug.Log(hit.point);
Debug.DrawLine(ray.origin, hit.point,Color.yellow);
smokeMover.transform.position += curVec.normalized * Time.deltaTime * moverSpeed;
}
else
{
Debug.Log("COLLISON");
}
Try drawing a debug ray the same length as your collision ray to see how far the ray is being projected. It's possibly just too long.
how would i go about doing that? i have a simple check which just shows me the ray of the object co$$anonymous$$g out but that seems to be a correct distance.
Debug.DrawRay(ray.origin, ray.direction, Color.blue, 0, false);
Draw the ray to the hit point. Your distances are different in your rays.
From the docs:
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
I have no idea what all that curvec stuff or what the out word is for (I'm a pretty basic coder) but maybe you're complicating things somehow?
Ummm, you are drawing the line at the moment when there is no collision so it will definitely not be showing you anything useful - remove the ! from the raycast call and switch your debug logs about collision and no collision.
Answer by POLYGAMe · Feb 20, 2014 at 09:29 PM
HAHAHA! I totally missed the "!". Yup, that'll do it!
User solved question from recommendations in comments.