So let’s understand the basics of raycast in unity. So here are the variable and functions that we want to understand.
Ray:-
“A ray is an infinite line starting at the origin and going in some direction“.
A ray contains the details of a ray i.e. origin point and direction. It represents in a struct data format.
RaycastHit:-
“Structure used to get information back from a raycast.“
When a ray hits any gameobject at that point RaycastHit contains the data of that gameobject. It allows us to use that data to perform any action we want.
Physics.Raycast() :-
“Casts a ray, from point origin, in direction, of length maxDistance, against all colliders in the Scene.“
It is the function that casts the ray in unity.