- Home /
The question is answered, right answer was accepted
Physics.Raycast not detecting objects between camera and player
I'm trying to make a script that disables the mesh renderers of objects that are between the player and the camera, so that if the camera moves through a wall, the player is still visible, since the object will be invisible (but still active). I've created a Physics.Raycast, but it isn't detecting walls that go between the player and the camera. Here's the script:
void FixedUpdate ()
{
//reenable all object renderers that have been disabled
if (disabledObjects.Count > 0)
{
Debug.Log ("list had at least one object in it");
foreach (GameObject gObject in disabledObjects) {
Renderer rend;
rend = gObject.GetComponent <Renderer> ();
rend.enabled = true;
}
}
//clear the list, setting up to be refilled by new objects between camera and player.
Debug.Log ("clearing list");
disabledObjects.Clear ();
//while loop to ensure each object detected by the raycast is disabled
RaycastHit hitInfo;
while (Physics.Raycast (transform.position, target.transform.position, out hitInfo))
{
Debug.Log ("there was something between the camera and the target");
Renderer rend;
rend = hitInfo.collider.GetComponent <Renderer> ();
rend.enabled = false;
disabledObjects.Add (hitInfo.collider);
}
}
What did I do wrong?
I'm pretty much completely new to Unity, please don't eat me if this is a stupid question.
Answer by Sisym · Jun 29, 2015 at 04:55 PM
Ignore my previous comment (I can't delete it, since it hasn't been approved yet). I solved the problem. The problem was that I thought a ray was drawn between two points, so I simply gave it the position of the camera and the player, but I solved it when I realized the second argument was direction, rather than another point. I also figured out that I had to turn gizmos on in order to use Debug.RaycastDraw.
I definitely would not have been able to figure out Physics.RaycastAll though, thanks for the help!
Answer by Hellium · Jun 29, 2015 at 09:48 AM
The while loop won't do what you want.
Use Physics.RaycastAll instead of Physics.Raycast to get all the objects touched by the ray. In fact, disabling the mesh renderer won't let the ray pass through your object.
http://docs.unity3d.com/ScriptReference/Physics.RaycastAll.html
Okay, so I used the Physics.RaycastAll method ins$$anonymous$$d of the while loop, and still got the same problem where it wasn't detecting objects between the player and the camera. I solved the issue by reversing target.transform.position and transform.position, but now the raycast is detecting objects that are in front of the player by about ~1.3 units. Debug.RaycastDraw isn't drawing anything for me, so I can't debug that way either.
So, why did reversing the transforms of the player and the camera solve the problem, and why is my raycast detecting objects clearly not between the player and the camera?
Thanks.
I haven't notice but yes, you are right, a ray is defined by an origin and a direction.
Thanks for the feedback, don't forget to accept the answer by clicking on the check mark under the vote buttons ! ;)
You didnt get the answer dude.
But you certainly deserve some thumbs up, Hellium.( @Sisym this is your job )
Follow this Question
Related Questions
Shoot on spheres 0 Answers
event triggered by the wrong camera 0 Answers
Physics.Raycast lagging behind 1 Answer
Crosshair Distance Indicator 1 Answer