Forum Discussion
nigrin
13 years agoExplorer
[TIP] Detecting where the player looks at
Hi guys, we came across this for our vrjam game "Sorcerer's Wrath", where the player fights things by just looking at them. Here is a quick blog post how we did this, I have not seen this specif...
Anonymous
12 years agoIn the Unity tutorials, they use raycasting instead of a collider like Nigrin suggested.
I think (and may be wrong) that the problem of the collider is that you can "look" at something through walls. Let's say you look forward, at a wall, and right behind this wall, an enemy enters the collider : the
is true, even if you don't see the enemy, and somecode will be executed.
With the raycast, you can put something like :
in your code and it will take the first thing the raycast hits. So, if it's a wall, the condition is false even if your raycast hits an enemy behind the wall.
I personally use linecast instead of raycast if possible, because I had less problems with it, and because I think that it's faster (not having to calculate the ray to the infinite), but maybe am I wrong on this. The best way I found to do what we intend to do is to combine both approaches : use a box collider to see if there is an enemy in front of you (or near you), even if it's behind a wall, and if there is one, cast a raycast (or linecast) to see if there is something between you and the enemy. This way, you can avoid the "enemy behind wall" problem, and you don't use constantly raycasts / linecasts, and that should be better for the performance of the game.
I think (and may be wrong) that the problem of the collider is that you can "look" at something through walls. Let's say you look forward, at a wall, and right behind this wall, an enemy enters the collider : the
onColliderEnter(collider other){if(other == enemy){somecode();}}is true, even if you don't see the enemy, and somecode will be executed.
With the raycast, you can put something like :
raycasthit = hit ;
Physics.Raycast(transform.position, Vector3.forward, out hit);
if(hit == enemy){somecode();}
in your code and it will take the first thing the raycast hits. So, if it's a wall, the condition is false even if your raycast hits an enemy behind the wall.
I personally use linecast instead of raycast if possible, because I had less problems with it, and because I think that it's faster (not having to calculate the ray to the infinite), but maybe am I wrong on this. The best way I found to do what we intend to do is to combine both approaches : use a box collider to see if there is an enemy in front of you (or near you), even if it's behind a wall, and if there is one, cast a raycast (or linecast) to see if there is something between you and the enemy. This way, you can avoid the "enemy behind wall" problem, and you don't use constantly raycasts / linecasts, and that should be better for the performance of the game.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 3 years ago
- 9 months ago