Uploaded June 2022 | Updated November 2026, 3 hours ago
Hello again internet! Let's dive into how to manage camera physics in your third person cameras using `SphereCast`s.
`SphereCast`'s expand upon what you might know about raycasts projecting a beam through your scene that can detect physical objects in the scene and let you know if it's hit them. Unlike a raycast though a `SphereCast` includes a radius. This can prevent the detection from going through tight spaces or between closely aligned Game Objects.
To showcase this, we're going to build a quick 3rd Person Camera in a scene that has some narrow alleyways. In order to prevent our camera from clipping geometry inside those alleyways and to prevent it from ending up in a narrow space we'll project a sphere using a `SphereCast`. This gives our camera some safety and improves the quality of the camera positioning.
You can find more information about `SphereCast`'s in Unity's documentation here: docs.unity3d.com/ScriptReference/Physics.SphereCast.html
***
The code we wrote in this video is here if you'd like to try it yourself!
```csharp
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
{
public float cameraSpeed;
public Transform cameraTransform;
public float cameraDistance;
public float radius = 0.25f;
void Update()
{
// Multiplying quaternions adds them together
this.transform.rotation *= Quaternion.Euler(0, cameraSpeed * Input.GetAxis("Mouse X"), 0);
// Move Camera back until we get near a Physics Collider
Ray ray = new Ray(this.transform.position, -this.transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, radius, out hit, cameraDistance))
{
cameraTransform.localPosition = Vector3.back * hit.distance;
}
else
{
cameraTransform.localPosition = Vector3.back * cameraDistance;
}
}
// Draw a Gizmo around where the camera has been projected to.
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(cameraTransform.position, radius);
}
}
```
Looking for an introduction to `Raycast`s? Check out my video on Raycasting in Unity: worldofzero.com/videos/detecting-objects-with-raycasts-in-unity3d
If your interested in some of my other Cloud based videos I stream at twitch.tv/MicrosoftDeveloper regularly and some of my other videos can be found on the Microsoft Reactor YouTube channel: youtube.com/c/MicrosoftReactor1
I currently run events out of the Microsoft Reactor in San Francisco as well! If your nearby and want to learn things or join a workshop in person checkout our meetup group. meetup.com/Microsoft-Reactor-San-Francisco
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
Hello again internet! Let's dive into how to manage camera physics in your third person cameras using `SphereCast`s.
`SphereCast`'s expand upon what you might know about raycasts projecting a beam through your scene that can detect physical objects in the scene and let you know if it's hit them. Unlike a raycast though a `SphereCast` includes a radius. This can prevent the detection from going through tight spaces or between closely aligned Game Objects.
To showcase this, we're going to build a quick 3rd Person Camera in a scene that has some narrow alleyways. In order to prevent our camera from clipping geometry inside those alleyways and to prevent it from ending up in a narrow space we'll project a sphere using a `SphereCast`. This gives our camera some safety and improves the quality of the camera positioning.
You can find more information about `SphereCast`'s in Unity's documentation here: docs.unity3d.com/ScriptReference/Physics.SphereCast.html
***
The code we wrote in this video is here if you'd like to try it yourself!
```csharp
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour
{
public float cameraSpeed;
public Transform cameraTransform;
public float cameraDistance;
public float radius = 0.25f;
void Update()
{
// Multiplying quaternions adds them together
this.transform.rotation *= Quaternion.Euler(0, cameraSpeed * Input.GetAxis("Mouse X"), 0);
// Move Camera back until we get near a Physics Collider
Ray ray = new Ray(this.transform.position, -this.transform.forward);
RaycastHit hit;
if (Physics.SphereCast(ray, radius, out hit, cameraDistance))
{
cameraTransform.localPosition = Vector3.back * hit.distance;
}
else
{
cameraTransform.localPosition = Vector3.back * cameraDistance;
}
}
// Draw a Gizmo around where the camera has been projected to.
void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(cameraTransform.position, radius);
}
}
```
Looking for an introduction to `Raycast`s? Check out my video on Raycasting in Unity: worldofzero.com/videos/detecting-objects-with-raycasts-in-unity3d
If your interested in some of my other Cloud based videos I stream at twitch.tv/MicrosoftDeveloper regularly and some of my other videos can be found on the Microsoft Reactor YouTube channel: youtube.com/c/MicrosoftReactor1
I currently run events out of the Microsoft Reactor in San Francisco as well! If your nearby and want to learn things or join a workshop in person checkout our meetup group. meetup.com/Microsoft-Reactor-San-Francisco
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
.
Explore more about FFmpeg at the official website https://ffmpeg.org
Learn how to install FFmpeg: https://ffmpeg.org/download.html
If youd like to use FFmpeg for other things (like converting video snippets to GIFs) check out this blog post from GIPHY: https://engineering.giphy.com/how-to-make-gifs-with-ffmpeg/
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Quickly Trim Video Snippets on the Command Line with FFmpeg](https://i.ytimg.com/vi/Cg2omJAOH30/mqdefault.jpg)


.
Moq is a framework for .NET that makes it easy to generate and test mocked code. Given a class you are able to replace that class with a clone that matches all of the type requirements of the class but has none of the implementation leaving the rest up to you. It is compatible with most testing frameworks, but because were hoping to later integrate with Unity 3D as part of our [Platforming game](https://www.youtube.com/playlist?list=PLEwYhelKHmigkz97T76r2bCt7wfG_1pPE) well be testing our integration of Moq with [NUnit](https://nunit.org/).
This framework is new to me so lets explore it together and cover some of the basic use cases that Id like to employ Moq for: Mocking the value of object Properties, Mocking the value of object Functions and Mocking the results of a specific function invocation. From that hopefully well be able to build a better understanding of the capabilities of Moq and how we can best use it to improve the quality of our testing.
***
To add Moq to your dotnet project you can run:
```ps1
dotnet add package Moq
```
Moq is also on GitHub. You can browse the source and see code samples there: https://github.com/moq/moq4
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Im Learning Moq - A .NET Mocking Framework for Unit Tests](https://i.ytimg.com/vi/EJZ2xWGfFQw/mqdefault.jpg)
 in an earlier video series. You can accomplish a lot with basic distance formula to create a number of very cool effects!
If you have suggestions for how we could improve this shader or ideas on what we should do with it next let me know in the comments!
Join the World of Zero Discord channel: https://discord.gg/hU5Kq2u A Static Glowing Ring - Ripple Shader - Part 1](https://i.ytimg.com/vi/EhkBdCdNx-Y/mqdefault.jpg)





 for recommending the use of `ContextMenu`s. You can find more of their content as a guest in videos at Infallible Code: https://www.youtube.com/user/charlesamat
### Code Examples
Some example uses of Unity 3Ds `ContextMenu` and `ContextMenuItem` Attributes:
#### `ContextMenu`
A `ContextMenu` Attribute is attached to functions in your `MonoBehavior`. When that menu item is clicked it will invoke the function the attribute is assigned to:
```csharp
[ContextMenu(Context Menu Example)]
void HelloContextMenu() {
Debug.Log(Context Menu Clicked!);
}
```
#### `ContextMenuItem`
A `ContextMenuItem` Attribute is attached to a field and requires you to provide the name of a function that will be invoked when the menu item is clicked.
```csharp
[ContextMenuItem(Context Menu Item Example, nameof(InvokeContextMenuItem))]
public float someVariable;
void InvokeContextMenuItem() {
Debug.Log(Context Menu Item Clicked!);
}
```
[`nameof`](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/nameof) is a C# function that returns a string representation for the name of the class, function, field, property etc that is referenced. Using this allows things like refactorings to function better and provides compiler errors if you spell a function name incorrectly (`nameof(FunctionName)` will throw an error if `FunctionName` is not defined).
Note: The functions invoked by both `ContextMenu` and `ContextMenuItem` should not have any arguments.
***
Unitys documentation on the `ContextMenu` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenu.html
Unitys documentation on the `ContextMenuItem` Attribute: https://docs.unity3d.com/ScriptReference/ContextMenuItemAttribute.html
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Using ContextMenu and ContextMenuItem in Unity 3D](https://i.ytimg.com/vi/GKG-vUFt3D4/mqdefault.jpg)