What is Raycast?

Can Someone please explain me what is a ray cast and how it is used in Games?
Sorry to be a noob but I don’t understand it

1 Like

In a shooter game like counter strike, when you shoot, the bullet is no a gameObject travelling fast, but is a “Ray” from the gun to N distance. And every player in this ray get damaged.

Is my example clear ?

5 Likes

https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

  • Ray is where something starts at a point, and then creates a line (you cannot see) in some direction away. The idea is that it then follows this line to see if it collides with anything.

In Physics, the collision is used to detect that something is that direction, or finding out whats in front. I.e. if you have a space ship flying forward, and a raycast from the ship forward collides with an asteroid. Now your AI makes the ship go turn or stop.

In Rendering, a RayCast is an operation from the camera point sending out a “ray” find the surface it collides with and then render the material on that surface for that pixel in the screen. Additionally, light sources may cast rays, and see if that shows a reflection on that material.

There is a lot that rays can do. I strongly suggest following the tutorial to learn more about it.
https://unity3d.com/learn/tutorials/modules/beginner/physics/raycasting

8 Likes

Read the docs first: Unity - Scripting API: Physics.Raycast
It’s simply a way to shoot a line and see if it hits a collider. If it hits a collider you can get useful information about it. It’s used for gathering information in 3D space using the physics engine.

You can use it to answer questions similar to:

“Is there an object behind me?”

“What is the 3D position of what my line touches?”

“What angle is the surface where my ray touches?”

And so on… lots of information. That’s the purpose of using them.

5 Likes