Raycast (422176)

Hi,

I was just wondering how ray casts work, I want it so that a raycast comes out of my enemies eyes, so it can only see the player when facing it, so that if you weant you can creep up on the enemy.

How could I do this?

A raycast is a ray or line segment with a defined starting point and direction. In your example, it starts at the object’s eyes and goes a defined distance (line segment in this particular case) in the direction the character is facing.

To use it, you should read up on RayCastHit as well. I looked for some good examples, but didn’t find any very quickly.

A raycast is also infinitely thin.

And for that reason, rays are a poor choice. Instead, you can get the angle between the enemy’s look direction and the position of the player. If the player is within a certain cone, say 60 or 90 degrees or whatever, he’ll be seen.

One quick thing to eliminate raycast:

float distance = Vector3.Distance (object1.transform.position, object2.transform.position);

This detects the distance between two objects.