Uploaded December 2020 | Updated November 2026, 3 hours ago
Raycasts provide a way for you to project lines through your scenes and detect the objects they hit as well as return important information about what they hit. This gives you a way to detect if a projectile will impact a surface, test if one player can see another, simulate a laser pointer and more.
There are two main ways of raycasting in Unity. The first behaves like a laser pointer being cast out from an origin point and stopping once it encounters a single object (or its maximum range). The other option uses the `Physics.RaycastAll` and returns a set of all objects that would be impacted across the entire length of the ray.
Raycasts may be made by providing just an origin point and the direction. This returns true if any object is in the path of the ray, otherwise it results in false. For example the code to see if an object is in front of the attached Game Object might look like this:
```csharp
bool hitDetected = Physics.Raycast(this.transform.position, this.transform.forward);
```
Raycasts return a boolean representing if they hit an object or not. If you would like to learn more about what your ray hit you'll need to take advantage of the [`RaycastHit`](docs.unity3d.com/ScriptReference/RaycastHit.html) object. This provides information about the collider you impacted, the point of impact, that objects transform and more. Because of the way values are returned a typical pattern of using Raycasts looks like this:
```csharp
var ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
objectHit = hit.transform.gameObject;
}
```
### Raycasting Examples
Here is a collection of a few ways you could apply raycasts from my previous projects:
* [Obstacle Avoidance and Steering Algorithms](youtu.be/SVazwHyfB7g)
* [Reflecting Projectiles Off Walls](youtu.be/GttdLYKEJAM)
* [Adding Gravity and Trajectory Prediction](youtu.be/d7pwmO6IS2I)
* [Projectile Penetration and Shooting Through Walls](youtu.be/RIru7m73HZA)
* [Help Debugging Rays with Unity Gizmos](youtube.com/watch?v=USwrSUpIgLI)
### Raycast Alternatives
Unity includes a set of functions that can be used in place of raycasts that use different shapes such as spheres or boxes to detect collisions. These function similarly to raycasts for the most part besides having a different shape.
#### [`SphereCast`](docs.unity3d.com/ScriptReference/Physics.SphereCast.html)
`SphereCast` projects a sphere along a ray effectively allowing your raycast to have a radius around itself when detecting objects. This has a few uses but one of the most common is in the development of [third person cameras](youtu.be/Cfcr7FtbYQo) to prevent them from clipping into level geometry and ensure they behave more cleanly in tight corners.
#### [`BoxCast`](docs.unity3d.com/ScriptReference/Physics.BoxCast.html)
`Boxcast` projects a box along the ray and allow you to test if a box may move without colliding with an object.
#### [`CapsuleCast`](docs.unity3d.com/ScriptReference/Physics.CapsuleCast.html)
`CapsuleCast` projects a capsule along a ray and can be useful for detecting things like potential player movement.
#### [`LineCast`](docs.unity3d.com/ScriptReference/Physics.Linecast.html)
This functions identically to a `Raycast` however instead of expecting an origin and direction and `LineCast` expects two points and detects if an object is between those two points.
### 2D Raycasts
Variants of the 3D Raycasts and its alternatives can be used when your using Unity's 2D Physics system. These have a different syntax and the two raycasting systems are *not* intercompatible. `Physics.Raycast` will interact with 3D Colliders, `Physics2D.Raycast` will interact with 2D Colliders. To learn more about using Raycasts in a 2D scene see the documentation for [`Physics2D.Raycast`](https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html).
***
The Raycasting documentation is here: docs.unity3d.com/ScriptReference/Physics.Raycast.html
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
I've published additional content on my website at worldofzero.com
You may find some of the World of Zero Open Source projects at github.com/WorldOfZero
Raycasts provide a way for you to project lines through your scenes and detect the objects they hit as well as return important information about what they hit. This gives you a way to detect if a projectile will impact a surface, test if one player can see another, simulate a laser pointer and more.
There are two main ways of raycasting in Unity. The first behaves like a laser pointer being cast out from an origin point and stopping once it encounters a single object (or its maximum range). The other option uses the `Physics.RaycastAll` and returns a set of all objects that would be impacted across the entire length of the ray.
Raycasts may be made by providing just an origin point and the direction. This returns true if any object is in the path of the ray, otherwise it results in false. For example the code to see if an object is in front of the attached Game Object might look like this:
```csharp
bool hitDetected = Physics.Raycast(this.transform.position, this.transform.forward);
```
Raycasts return a boolean representing if they hit an object or not. If you would like to learn more about what your ray hit you'll need to take advantage of the [`RaycastHit`](docs.unity3d.com/ScriptReference/RaycastHit.html) object. This provides information about the collider you impacted, the point of impact, that objects transform and more. Because of the way values are returned a typical pattern of using Raycasts looks like this:
```csharp
var ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
objectHit = hit.transform.gameObject;
}
```
### Raycasting Examples
Here is a collection of a few ways you could apply raycasts from my previous projects:
* [Obstacle Avoidance and Steering Algorithms](youtu.be/SVazwHyfB7g)
* [Reflecting Projectiles Off Walls](youtu.be/GttdLYKEJAM)
* [Adding Gravity and Trajectory Prediction](youtu.be/d7pwmO6IS2I)
* [Projectile Penetration and Shooting Through Walls](youtu.be/RIru7m73HZA)
* [Help Debugging Rays with Unity Gizmos](youtube.com/watch?v=USwrSUpIgLI)
### Raycast Alternatives
Unity includes a set of functions that can be used in place of raycasts that use different shapes such as spheres or boxes to detect collisions. These function similarly to raycasts for the most part besides having a different shape.
#### [`SphereCast`](docs.unity3d.com/ScriptReference/Physics.SphereCast.html)
`SphereCast` projects a sphere along a ray effectively allowing your raycast to have a radius around itself when detecting objects. This has a few uses but one of the most common is in the development of [third person cameras](youtu.be/Cfcr7FtbYQo) to prevent them from clipping into level geometry and ensure they behave more cleanly in tight corners.
#### [`BoxCast`](docs.unity3d.com/ScriptReference/Physics.BoxCast.html)
`Boxcast` projects a box along the ray and allow you to test if a box may move without colliding with an object.
#### [`CapsuleCast`](docs.unity3d.com/ScriptReference/Physics.CapsuleCast.html)
`CapsuleCast` projects a capsule along a ray and can be useful for detecting things like potential player movement.
#### [`LineCast`](docs.unity3d.com/ScriptReference/Physics.Linecast.html)
This functions identically to a `Raycast` however instead of expecting an origin and direction and `LineCast` expects two points and detects if an object is between those two points.
### 2D Raycasts
Variants of the 3D Raycasts and its alternatives can be used when your using Unity's 2D Physics system. These have a different syntax and the two raycasting systems are *not* intercompatible. `Physics.Raycast` will interact with 3D Colliders, `Physics2D.Raycast` will interact with 2D Colliders. To learn more about using Raycasts in a 2D scene see the documentation for [`Physics2D.Raycast`](https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html).
***
The Raycasting documentation is here: docs.unity3d.com/ScriptReference/Physics.Raycast.html
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
I've published additional content on my website at worldofzero.com
You may find some of the World of Zero Open Source projects at github.com/WorldOfZero
![Using Command Prompt to Create a Symbolic Link on Windows
Symbolic Links allow you to create a reference in your file system to another file or directory. This allows you to reference the same file in multiple places without having to physically copy it into each place. This can save storage space and also allows a single change to the file to be seen by every location that links to it as well.
In this video well be taking a look at how to create these Symbolic Links from the Command Prompt using the `mklink` command. In order to use this command youll need to be running Command Prompt as an administrator. `mklink` can be used to create symlinks between files and directories however the syntax is slightly different depending on which youll be using.
When creating a symlink between files you may use:
```cmd
mklink [path] [target]
```
However if youre creating a symlink between directories youll need to include the `/D` flag.
```cmd
mklink /D [path] [target]
```
In both of these examples a new file or directory will be created at the `path` that links to the object located at the `target`.
The `mklink` command also allows you to create hard links and directory junctions as well. You may learn more about using it by running `mklink -?` in your prompt.
If you see the You do not have sufficient privilege to perform this operation error make sure you are in an Administrator Command Prompt.
If you prefer Powershell you can learn how to make a symbolic link in Powershell here: https://youtu.be/_VnONfOgP8M
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using Command Prompt to Create a Symbolic Link on Windows](https://i.ytimg.com/vi/w2gxWTOPw7U/mqdefault.jpg)









 that give you the ability to declare a using directive over every source file in your project instead of needing to duplicate the declaration inside of each file in your projects! This can help you reduce the duplicated code in your projects and make commonly used libraries and namespaces more readily available across your project!
To use a global using you can add `global` before your using statements like this statement you might use in a data serializing/deserializing library:
```csharp
global using Newtonsoft.Json
```
Including a global using will cause the compiler to include the namespace when resolving types across the entire compilation of your project instead of scoping that reference to the file the using is a part of. What this typically means is that you will want to use this for common namespaces used across your project and continue using file based using statements when referencing more feature specific types.
You can find more information about using Global Usings as part of the using directive documentation here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-directive#global-modifier
Global Usings are also part of the `ImplicitUsings` feature you might see in your `.csproj` files. To learn more about this feature you can watch this video! https://youtu.be/NlvhBpLl0DY
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u
This videos thumbnail uses a globe SVG from here: https://commons.wikimedia.org/wiki/File:Emojione_BW_1F30E.svg Lets Explore Global Usings in C# 10](https://i.ytimg.com/vi/z4THCW3b16k/mqdefault.jpg)