Handling 3rd Person Camera Physics with SphereCasts @WorldOfZeroDevelopment
Handling 3rd Person Camera Physics with SphereCasts  @WorldOfZeroDevelopment
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
Handling 3rd Person Camera Physics with SphereCastsQuickly Trim Video Snippets on the Command Line with FFmpegCreating a Quick Custom Profile in the Windows TerminalDemoing the Tool and Adding Command History - LINQ for Unity Stream - Part 2Im Learning Moq - A .NET Mocking Framework for Unit TestsA Static Glowing Ring - Ripple Shader - Part 1Dependency Injection in SpringInline Initializing C# Lists - Quick TipRecreating Particle Effects with Game Objects in Unity 3DBuilding a Custom Node in Unitys Shader GraphRuntime C# Scripting in Unity UI - The Roslyn Compiler in UnityUsing ContextMenu and ContextMenuItem in Unity 3D
World of Zero |

Handling 3rd Person Camera Physics with SphereCast's

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER