Raycast, what the hell is that?

José Miguel Araújo de Medeiros
2 min readSep 22, 2019

--

There are a lot of useful tools when you’re programming with Unity, One of them is Raycast. A function that, in the basics of it, takes into account three variables: a origin point, a direction and a max distance. It casts a ray from the origin point to a direction and with a length equal to max distance.

As said it previously, the ray has a origin point, a direction and a max distance. The origin point is a position on space, therefore it is declared by a “Vector3”, since is used in a XYZ axis. As well as the origin point, the ray direction is another position on space, so it’s declared by a “Vector3” also. But the distance, being a single numeric value, is declared by a float variable type.

Beyond interact with objects, it can be used to render the map or terrain. Also, you got to know that rays trace the spectator’s eye perspective to the object.

Another utility is to know a object being in the spectator’s fov, or to check distances between the player and an object. Also in a shooter game, you can check if the enemy was hit without needing a object as a bullet. Usually used, as well, to get object’s positions where it was clicked.

Written by: José Miguel Araújo, João Vitor Araújo, Eduardo Chaves, Girlene de Andrade, Wilton Sales e Luciano Nascimento.

Based on: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html, https://unity3d.com/pt/learn/tutorials/topics/physics/raycasting, https://permadi.com/1996/05/ray-casting-tutorial-7/.

And special thanks to Erick Simões.

--

--