- Home /
Raycast doesn't work properly?
So my camera throws a Raycast forward, and if this raycasts target hits a specified target, i want it to tell me. Very simple. Altough, it doesn't seem to always tell me when the target gets hit even though i can clearly see it does get hit. I do use Debug.DrawRay so there shouldn't be any problem.
I do have this theory that i need to hit the exact pivot point or middle point of the object, but that shouldn't be neccesary. Did i miss something here?
Here's the code.
Debug.DrawRay(transform.position, transform.forward * 100f, Color.green, 20);
if(Physics.Raycast(transform.position, transform.forward, out Target, PlayerReachRange) && Target.transform.tag == "MovableObject")
{
Debug.Log ("Shiggidiboo");
}
Note that raycasts only hit Colliders on the object. Is your collider big enough?
What is the value of PlayerReachRange? Have you logged to to be absolutely sure? Are you sure, and have you verified with code, that the target tag is $$anonymous$$ovableObject?
The Collider covers the whole gameobject i'm trying to access. The value or PlayerReachRange is about 5 feet forward, and i know it is because sometimes but only somtimes it tells me it hits it. And i'm positive that $$anonymous$$ovableObject is the target tag. I've doubled checked. Gonna go do it again :)
The ray doesn't need to end inside the collider right? Because it feels like i'm missing something. Are you people having similar problems?
You should really use the PlayerReachRange variable in your draw ray :
Debug.DrawRay(transform.position, transform.forward * PlayerReachRange, Color.green, 20);
Also, split up your conditionals and do a debug to see what the ray hit :
Debug.DrawRay(transform.position, transform.forward * PlayerReachRange, Color.green, 20);
if(Physics.Raycast(transform.position, transform.forward, out Target, PlayerReachRange) )
{
Debug.Log ("Ray Hit " + Target.transform.tag);
if(Target.transform.tag == "$$anonymous$$ovableObject")
{
Debug.Log ("Shiggidiboo");
}
}
Your answer
Follow this Question
Related Questions
Make Ray hit own collider 1 Answer
Balancing Forces + Gravity 1 Answer
Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer
Physics2D.Raycasting questions 0 Answers
Held cube clips through wall 0 Answers