Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

Unity C# Third Person Camera Occlusion Script

Posted on March 18, 2025March 18, 2025 by Habibie

Here is a C# script for Unity that ensures a third-person camera moves closer to the player when an object obstructs the view. This script utilizes raycasting to detect occlusion and adjust the camera’s position accordingly.

using UnityEngine;

public class ThirdPersonCamera : MonoBehaviour
{
    public Transform player;
    public float defaultDistance = 5f;
    public float minDistance = 1f;
    public float smoothSpeed = 10f;
    public Vector3 offset;

    private float currentDistance;

    void Start()
    {
        currentDistance = defaultDistance;
    }

    void LateUpdate()
    {
        HandleCameraOcclusion();
    }

    void HandleCameraOcclusion()
    {
        Vector3 desiredPosition = player.position - transform.forward * defaultDistance + offset;
        RaycastHit hit;

        if (Physics.Raycast(player.position + offset, -transform.forward, out hit, defaultDistance))
        {
            currentDistance = Mathf.Clamp(hit.distance, minDistance, defaultDistance);
        }
        else
        {
            currentDistance = defaultDistance;
        }

        Vector3 newPosition = player.position - transform.forward * currentDistance + offset;
        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * smoothSpeed);
        transform.LookAt(player.position + offset);
    }
}

How It Works:

  1. The camera starts at a default distance from the player.
  2. A raycast is fired from the player toward the camera.
  3. If the ray hits an obstacle, the camera moves closer, adjusting its position to maintain visibility.
  4. If no obstacle is detected, the camera stays at the default distance.
  5. The camera smoothly transitions to the new position.

You can adjust:

  • defaultDistance for normal camera distance.
  • minDistance to set the closest the camera can get.
  • smoothSpeed to control movement smoothness.
Post Views: 353
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme