Raycast Explanation

Good morning everyone,

I was surfing on web, and i have see raycast, i searched a lot on the unity scripting reference, but nothing explain this nicely.

I have see that we can use for ScreenToRay, what is that?

we can use for detecting if is in the range, how? and why dont use vector3.distance?

we can use for enemy ai scripting? how?

we can use for shooting projectiles? but why dont use instantiation, and apply force on rigidbody?

what is really this raycast? what can i do? and how?

what is the scope? thanks everyone, please tell me more then the web on raycasts!!

thanks have a nice day!

Raycast projects a ray (a line or line segment) from a point in 3d space. If that ray hits a collider, it returns information about what it collided with. It can be used for many applications, including the ones you have listed.

To answer all your question …

“why dont use vector3.distance” because you do not know what the thing is, or where the thing is. (if you know two points, sure, use distance.) but if you don’t know WHAT you are pointing to, you need to find out.

When (example) someone touches a screen, you DO NOT KNOW what is under the finger. this is how you find out. When your character looks out from behind a door, you DO NOT KNOW what direction he is pointing at, this is exactly how you find out. You see? it’s like A.I., the first step of A.I.

“enemy ai scripting” … yes you often use it in many ways for that

“we can use for shooting projectiles” … not usually, you just use “instantiation, and apply force on rigidbody”

BUT if you are shooting a “laser” or “instant” bullet (the bullet is not actually an object flying through the air, it just does damage in a straight line, you use a cast to calculate the impact point

“what is the scope” it can touch anything, or only certain “physics layers” of the scene - learn about physcis layers (very simple and powerful)

Hope it helps!