- Home /
Need Help with RayCast. No Vector?
I'm testing some script, and I've noticed in scene view, the ray will not cast out from the camera all the time. There some extra stuff in my code that doesn't get executed, because the ray no longer hits the object. It seems to stop shooting out a vector.
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit, 100))
{
Debug.DrawLine(transform.position, hit.point, Color.blue);
if (hit.collider.gameObject.tag == "object")
{
// Do stuff here
Debug.DrawLine(transform.position, hit.point, Color.red);
}
}
}
I attached a picture of the scene view so you can see what I'm talking about. The line is blue while its being cast out and hitting random objects. But when I hit the cube, with the tag 'object', it turns red UNLESS I'm right up on the cube. Once I approach the cube too close, it stops everything. It's simple script, I'm not seeing why it's not working.
You need to figure out if it is not hitting anything, or if it is hitting something you don't expect. Put this inside of your Raycast() 'if' statement:
Debug.Log(hit.collider.name);
Agreed it's probably hitting something, just not your object
Answer by Skeletim · Jul 06, 2013 at 07:24 PM
Thanks. That line of code helped me track down the problem on what it was hitting.
Your answer
Follow this Question
Related Questions
RaycastHit.transform/collider 1 Answer
Is there a way to detect if an object is being hit by a raycast? 2 Answers
Fall collision force with help of raycast 0 Answers
How to get information from raycast? 2 Answers
Click&Drag Misterious Disappearing! 1 Answer