- Home /
Raycast not hitting collider
Hello, I am trying to add spotting similar to battlefield 3 and 4. All the players have a box collider around them that is on the layer "tagging." I have the player send out a raycast whenever they press 'E' and a layermask on the ray so that it only collides with the tagging layer. It works fine when I am relatively close to the player. However, when I get more than 10-50 units away from him, it doesnt detect the collision at all. Also, the raycast maxDistance is over 600 so that shouldnt be the problem.
Code:
void RpcSendRay(){
RaycastHit shot;
Debug.Log (transform.name + " sent ray"); //this is always logged
if(Physics.Raycast (raycastItem.position, raycastItem.forward, out shot, 600f, mask.value)){
Debug.Log (shot.transform.name + " ray hit"); //I never get to this point unless really close
Debug.Log (shot.distance);
PlayerTagging pt = shot.transform.GetComponent<PlayerTagging> ();
if (pt != null) { //this if statement just makes sure they have the script and then tags them
if (pt.GetComponent<Player> ().isLocalPlayer) {
return;
}
Debug.Log (shot.transform.name + " getting tagged");
pt.tagMe ();
}
}
}
Answer by Arustyred · Dec 26, 2017 at 01:21 AM
Alrighty, I got it. I'm not sure why but it is because I send out the ray in the clientRpc. I just moved it so that it is called locally and then it calls a command/rpc if it actually hits someone.
Answer by Bieere · Dec 25, 2017 at 07:09 PM
It could be that your forward vector is offset slightly, and could explain why it works at a close distance, and not at a further one, where the ray is actually going in a direction around the gameObject you're attempting to hit.
To see if this is in fact the issue, normalize your forward vector, and see if the issue is still there.
if(Physics.Raycast (raycastItem.position, raycastItem.forward.normalized, out shot, 600f, mask.value))
{
[...]
}
You should also do a Debug.Ray()
to see where exactly the ray is going
Thanks for responding. I have that in my Update function now and it is pointing straight. Any other ideas?
Your answer
Follow this Question
Related Questions
How can I let the ray to the edge of the capsule? 1 Answer
Raycast exit point of collider 0 Answers
Restrict Rigidbody movement in the Z axis 1 Answer
Fixing Editor Mouse Offset Due To Local Raycast? 1 Answer
SInce when?!?!? (Raycast issue) 2 Answers