- Home /
Negative raycast direction always returns true
Greetings, fine fellows!(And ladies)
My character is currently using a rigidbody, which works out very nicely, aside from collision issues where you can force your way through walls and whatnot.
The solution I'm considering for this is to have a ray fired in front of the character and one fired behind the character, which will check for walls on either side. The issue I'm experiencing with this is that, when doing the ray firing behind the player using a negative direction like so:
if(Physics.Raycast(transform.position, -transform.right, 1)) or if(Physics.Raycast(transform.position, -Vector3.right, 1)) etc.
It will always return that it's hitting something whenever the distance is greater than .02 (Which is far too short to be useful in my case)
Any insight on the issue?
Answer by Joshua · May 30, 2011 at 04:10 PM
You're probably raycasting from inside the character and then hitting the sides of the character. Try raycasting from outside the character or use layers. An example can be found in the DontGoThroughThings.js script.
Ah yeah, that makes sense. Thanks, Josh! I assume I could use tags as well?
You could use tags, but you have to check them manually. Layerbased raycasts ignore everything that is excluded in the layermask. Just place your player on a seperate layer and exclude this layer when you do a raycast.
Your answer
Follow this Question
Related Questions
Raycast Hit Question 1 Answer
MachineGun script applied damage to the object that is not hit. 1 Answer
Raycast in the wrong way 3 Answers
Generate a random direction within min and max angles 2 Answers