- Home /
Problem getting the right raycast hit
I've got a game object at about +10 Y that triggers, among other things, a Raycast when the 'player' gameobj breaks the collider. It has a spotlight on it which I have tracking the gameobject, and to try and understand when the gameobject has 'line of sight' on the player, I implemented a ray cast.
But something isn't right. Though I am staying on the player object with both the spotlight and a debug ray, when (I think) using the same targeting approach with the raycaster .- it only seems to register hits on the player object at a pretty dramatic distance.
In the images here, the spotlight is green simply when the object is tracking the player, but turns red while the raycaster is registering hit. As you can see, even though both the debug ray and spotlight have no problems hitting the player, the raycaster is pretty spotty about it - or appears to be..
First, the images: 
This one shows the raycast hitting the player at a pretty extreme distance, however this one:

Shows the spotlight and the debug ray hitting the player, but the color of the light (and a public bool, and debug messages) show that the ray is not hitting it. (blocked by something else maybe?)
In short, extensive checks indicate the debug ray and spot are both hitting - ray cast is not. Do raycasts stop at the first thing they hit? My understanding was they kept going and returned 'hits' on everything in their path.
Here is the code I am using to a) follow the player with the spot, b) cast a debug ray and c) the raycaster itself.
void LookAtTarget(Transform t)
{
Vector3 targetDir = t.position - scanrayHousing.position;
float step = 10f * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(scanrayHousing.forward, targetDir, step, 0.0F);
scanrayHousing.rotation = Quaternion.LookRotation(newDir);
RaycastHit hit;
if (Physics.Raycast(scanray.position, newDir, out hit))
print("Ray hit-->" + hit.transform.gameObject.name + " at " + hit.distance.ToString());
Debug.DrawRay(scanray.position, newDir * 10, Color.green);
if (hit.transform.gameObject.tag == "Player")
{
UpdateScanRay(pursuitColor);
lineOfSite = true;
}
else
{
UpdateScanRay(alertColor);
lineOfSite = false;
}
}
Keep in mind, I have been messing with this quite a bit, changing the transform position originating the spot, debug ray etc - all have produced variations on the same thing but for posterity:
the object is a simple sphere (scanray) and as a child, a empty game object which acts as the parent to the spotlight (scanrayhousing). I did these as a way of segregating the rotation of the parent object from the child.
Any help would be appreciated - I'm certain I am missing something elementary.
P.S. The raycaster does return hits, and does hit the player object, but with a huge angle of space below the object where it cannot seem to hit the player object.
The raycast stops at the first hit. If you want all hits use Phydics.RaycastAll.
Answer by aldonaletto · Jun 22, 2017 at 03:46 AM
Raycast stops at the first collider it hits, so if any collider is in between, the ray won't hit the player - but your code should show the name of the object which's blocking the ray. Part of your code should be inside the if - the way it is now keeps returning old info stored in the hit variable even when nothing is hit. You can let the debug ray outside the If, so that you'll know what the ray is aiming at:
...
Debug.DrawRay(scanray.position, newDir * 10, Color.green); // move this line outside the if
if (Physics.Raycast(scanray.position, newDir, out hit)){
// only use the info stored in hit if the ray hit something:
print("Ray hit-->" + hit.transform.gameObject.name + " at " + hit.distance.ToString());
if (hit.transform.gameObject.tag == "Player") {
UpdateScanRay(pursuitColor);
lineOfSite = true;
} else {
UpdateScanRay(alertColor);
lineOfSite = false;
}
}
}
Thanks so much. I should have known that when the raycast hit was not an array.
So, what I did not mention was that the sphere has a rather large box collider that is offset from the sphere (imagine a large box being dragged along the ground), this collider is acting as the sensor to pick up player collides - well - because of its size, it was automatically the first thing being hit until the players angle vs the spheres angle (at about 10.y).
Your comment put me on the right track - when the player collision is detected, in the ontriggerenter event I'm going to immediately resize the collider so that the raycast has the proper angle of attack on the player.
Thank you again!
Your answer
Follow this Question
Related Questions
C# Raycast goes straight into the air 2 Answers
how to setup raycast firing system in this situation 1 Answer
if ( Physics.Raycast ( ray, out hit, Mathf.Infinity, LayerMask ) ) not working? 2 Answers
Raycast off empty gameobject rather then camera. 2 Answers
Set raycast on player camera or object? 0 Answers