- Home /
Raycast not stopping at a wall?
Hi,
I'm making a slender game, and I have a raycast to see if the character is visible and take away from health if he is,
but my problem is that the raycast is not stopping at a wall. Here is the coding, I think whatever my error is it is in the function CheckIfVisible. Any help is appreciated! Thanks!
What gets printed in the console when you un-comment the line :
Debug.Log( "" + hit.collider.gameObject.name );
What are the collider settings on the slenderman? Do you have any layers that have different settings?
Answer by Seth-Bergman · Nov 06, 2012 at 04:26 AM
EDIT:
Deleted previous answer, it wasn't right anyway...
Since this script is known to work, and the issue at hand is that you are not detecting the walls, then, assuming the wall IS BETWEEN the two positions (enemy and player), this should work, so long as your wall has a COLLIDER.
IF your wall ALREADY has a collider, I guess just about the only other possibility is that one of your coordinates (thePlayer.position or theEnemy.position) is not what you expect..
to see for sure, use Debug.DrawLine
add this line at the top of the Update:
Debug.DrawLine(theEnemy.position,thePlayer.position,Color.green);
and make sure your line is where you expect, and see if it passes through the COLLIDER of the wall
I don't understand, I had no problem with using linecast when I wrote this. I just ran the project again, and uncommented the line
//Debug.Log( "" + hit.collider.gameObject.name );
and the results were Terrain and Player, therefore if the collider name is Player then isVisible is true. For all other outcomes isVisible needs to be set to false before the script reaches the movement part.
That's why I left the line in there (for future people to uncomment and check the console).
I really am not sure why this isn't picking up your walls, all I can suggest is the basics which have already been suggested : do your walls have colliders, and are they layermasked/different layer to the raycast? And when you uncomment the Debug after the linecast, what is displayed in the console (just Player, or other objects come up)?
seems like a linecast COULD work here.. but you wouldn't be checking for the PLAYER, as the line is BETWEEN the player and the enemy, and checking for occlusion between the two.. so you would ins$$anonymous$$d be checking for anything OTHER than those two objects, right?
in which case, I would use a layermask to avoid seeing either player or enemy,..
In other words, the PLAYER is one of the endpoints of the linecast, so checking for the hit to have the name "Player" makes no sense here
To make it easy for writing a guide that was hopefully (but apparently not so) foolproof, I put the pick-up-paper script on the player, and everything else on the enemy (even the health system). Then I used name ins$$anonymous$$d of tag to make things easier too. I am not sure on your other points, from the enemy perspective : if the enemy is within the max visible range and is not occluded, then isVisible = true; For all other outcomes is false. The enemy is only interested in the player, so the only thing to be checking for is the Player, for all other conditions the enemy is either occluded to the player or too far away, and therefore isVisible = false;
As I said, the script is tested and working, while I only have player terrain and papers in the scene, the console debugs all these 3 objects, when the enemy is occluded to the player by these objects terrain or paper is printed, so the linecast works and returns the name of the gameObject it first collides with between player and enemy. I still fail to understand the issue when the script is tested and working. But it's all good, I continually learn from this 'site, suggestions thoughts and comments are welcome =] (but the script works!)
O$$anonymous$$, my mistake then, guess I'm underthinking this :O
guess I see how this is working now, might have helped if I'd have known where this came from too :)
On second thought, you're surely right, the walls must not have colliders :D
This is how the dot product and range zones are set up :
here is a package of the project :
http://www.alucardj.net16.net/unityanswers/SlenderGuideV1-scene0.unitypackage
create a new project, then import the above asset. Open the folder _Scenes and then open scene0. Use the E key in the editor to pick up papers so that ScreenLock doesn't break, but hopefully adding the edits to the pick-up-paper script will make it work if ScreenLock breaks =]
Your answer
![](https://koobas.hobune.stream/wayback/20220613083421im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect if GameObject is Touching an Imported Level 0 Answers
how to use raycasting for enemy sight (C#)? 3 Answers
Wall running using raycasting. 2 Answers
Going through wall 2 Answers