- Home /
Using a "Flashlight" as a form of Detection
Hello Unity Community,
I have been working with Unity for a little while, and have ran into a problem. I want to make a quick example of a "stealth", the basis of the game is that the enemies can only detect you if you are in their flashlight sight. After researching I found out that Sphere casting would be the best way to do this. However, I found none of the documentation helpful, and after looking at other examples I am still stumped on how to "detect" of the block is in the flashlight area.
Any help is greatly appreciated!
i've done it with 2 spherecasts - first defines range second the cone as it has center on the border of the first sphere at the position in the center of the view
@komodor - I take back what I said about Spherecast(). This should work.
hi sorry to point same thing out but can't you use raycast and use FOV caluclation to do that it is much more efficient and easier too
Answer by casualbanana · Apr 16, 2014 at 03:54 PM
I just figured it out actually, thanks anyways for the comments (well I actually stole concepts and pieces of code from other places). If anyone has an easier more robust way I would be glad to hear it!
public var radius = 1.0f;
public var distance = 10.0f;
function Start () {
}
function Update () {
var forwardRay = new Ray(transform.position, transform.forward);
var hits = Physics.SphereCastAll(forwardRay, radius, distance);
Debug.DrawRay(forwardRay.origin, forwardRay.direction * distance, Color.white);
for (var hit in hits)
{
Debug.DrawLine(forwardRay.origin, hit.point, Color.red);
Debug.DrawRay(hit.point, hit.normal, Color.blue);
if (hit.collider.tag == "Player")
print("player detected");
}
}