- Home /
c# Raycast and then checking tag
I'm willing to accept that this question has been asked a hundred times before, but all of the answers I've tried over the last hour or so haven't worked.
Basically I have a Raycast which goes beneath my character in order to check if he's touching the ground to enable him to jump again. I've since then added some trigger colliders to stuff to dampen velocity, but the raycast hits this trigger collider (which obviously I walk through not stand on) so it makes the jumping unusual. As such, I need to be able to get the raycast to ignore this. I'm trying to do this with tag. I have tagged the offending trigger collider "JumpStop", and I have added an if statement to the raycast to check that the tag of the object raycasted != "JumpStop".
It doesn't work, and I'm pretty sure it's because I'm not doing the raycast properly. Please help.
I have a variable declared at the start (GameObject hitInfo)
if (Physics.Raycast (transform.position, Vector3.down, out hitInfo, raycastLength))
{
if (hitInfo.collider.tag != "JumpStop") {
jumping = false;
}
}
Is there something obviously wrong here?
You can use Physics.RaycastAll() and check against all the hits. If the ground is a single object, you can use Collider.Raycast() to raycast against a single collider.
Answer by Kiwasi · May 09, 2014 at 09:17 PM
I just recently read another answer that solved the problem for me. Essentially there is a menu option to make raycasts ignore triggers
From @OrangeLightning
Either use layers or you might simply want to go to:
Edit -> Project Settings -> Physics -> Raycasts Hit Trigger
And set that to false.
I cannot believe I have not yet found that in my hours of google. Thankyou kind sir.
Answer by Jeff-Kesselman · May 09, 2014 at 08:30 PM
The right way to do a ray cast that ignores things is to put what you want the ray cast to hit in a different layer and then limit the cast to that layer.
But I want the raycast to hit everything that is below me, since it's just checking if I'm standing on something (the what the something is doesn't matter, just that it's not a trigger collider)
Well, you could put the object with the trigger collider in the IgnoreRaycasts layer.
Your answer
Follow this Question
Related Questions
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer
find tag from raycast 1 Answer
Applying damage to an object 1 Answer
GetComponent, gameObject.tag vs gameObject.layer 1 Answer
Exclude tag from raycast 1 Answer