# RayCasting and AI

Can someone explain how raycasting works and supply an example that’s better than my below code? This only run towards the target and looks at the target depending on distance. What would be a better solution. I’d like to have obstacle avoidance even a simple one.

I really want to understand this because I love programing and I find AI systems fascinating. How can I do the below with using raycasts?

`````` function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * damping);

transform.rotation = Quaternion.Euler(lockPos, transform.rotation.eulerAngles.y,
lockPos);

}

function Chasing ()
{

enemyObject.renderer.material.color = Color.red;

transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
``````

The idea behind RayCasting is to think of yourself inside the game. Unfortunately, you can’t see (you are blind) but you have a magic gun. You hold the gun in your hand and you fire it in a particular direction. Instantly the magic happens. The bullet that you fired from your gun is now returned to you and (because this is magic) the bullet talks. It says … hey … you fired me in this direction and here is information about the thing I hit. The bullet tells you all about the Game Object that it struck, where it struck it and how far away the Game Object was. If the bullet missed striking an object, it will also tell you that.

The bullet has more capabilities but this is the core of the story.

In computer graphics, there is a technique called “Ray Tracing” which is used to draw 3D rendered images. This works tracing paths from light sources that bounce off objects and may eventually reach a point which is the back of your eyes and hence be seen. Since our story here is not drawing complete 3D rendered images (using ray tracing), I believe the act of following a single “photon” of light (the bullet) is called ray casting.

Raycast is done mathematically: you supply a ray (or starting point and direction), an optional range distance and an optional reference to a RaycastHit structure, and the physics engine verifies which’s the closest (if any) object hit by the ray inside the specified range. If any object is hit, Physics.Raycast returns true and fills the RaycastHit (if specified) with info about the object, the hit point, normal etc.

But a ray is infinitely thin, thus for pathfinding purposes it’s better to use Physics.CapsuleCast: this function projects a capsule instead of a ray for the specified distance and direction; if it returns false, you can go ahead because the path is free - otherwise you must find another path (that’s the hard part…)

About your code: usually enemies are CharacterControllers, thus collisions are checked and they do not pass through obstacles. Adapted to CharacterControllers, your code could be:

```function lookAt (){
var dir = target.position - transform.position;
dir.y = 0; // keep only the horizontal direction
var rotation = Quaternion.LookRotation(dir);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}

function Chasing (){
enemyObject.renderer.material.color = Color.red;