How to stop 3rd person camera going throught walls ?

Hello I am strugling with a 3rd person camera controll.

I want the camera to get closer to the player when it hits the wall or any obstacle.

But i don’t know how
I’ve tried putting colliders onto it but it doesn’t work.

Does anybody knows how to fix this ?

This is my code →

  public float mouseSensitivity = 10;
    public Transform target;
    public float distanceFromTarget = 2;
    public Vector2 MinMaxViewAngle = new Vector2( -40,85);

    public float rotationSmoothTime = .12f;
    Vector3 rotationSmoothVelocity;
    Vector3 currentRotation;

    float Xrotation;
    float Yrotation;

    // Update is called once per frame

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }
    void LateUpdate ()
    {
        Xrotation += Input.GetAxis("Mouse X") * mouseSensitivity;
        Yrotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
        Yrotation = Mathf.Clamp(Yrotation, MinMaxViewAngle.x, MinMaxViewAngle.y);

        currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(Yrotation, Xrotation), ref rotationSmoothVelocity, rotationSmoothTime);
        transform.eulerAngles = currentRotation;

        transform.position = target.position - transform.forward * distanceFromTarget;
    }

Instead of using transform.position = target.position - stuff, you could raycast from the base position (which is “target.position” in your case) towards and up to the adjusted target position (which is target.position - transform.forward * distanceFromTarget, in your case).

If the raycast encounters anything that blocks it along the way, the hit position is where your camera should be. Plus maybe a tiny offset towards the base position.

Is this making any sense to you?

And actually, thinking of it, some probably have a better method for this. I could picture scenarios where the raycast might clear but there may be obstacles that are relatively close to the raycast and still obstructing the view.

1 Like

You should be able to put a collider on the camera to keep it from passing through walls. You can use the rigidbody function MovePosition to move it around while respecting physics. The rigidbody should be kinematic to avoid being part of the world simulation.

Thats a good idea. Thank you.

So basically call a raycast that if it hits anything infront of the camera the camera will move towards the player.

But what if the player is standing infront of the wall and the wall is behind the camera ? Is there a way how to “shoot” a raycast behind the camera so it moves when it hits the wall ?

No, other way around. Raycast from the player to where the camera should be. As long as the raycast doesn’t hit anything, the camera can be at its normal position. If the raycast hits something, it means that something is between the player and the normal camera position, and the camera should be adjusted.

Is this making anymore sense?

1 Like

If you’re going that way, you’ll want to do a boxcast to avoid having the camera fit through tiny spaces.

1 Like

Yes.Thank you.

1 Like

Thank you.

Sometimes you want the camera to stay behind objects filled with empty spaces (i.e. chainlink fence) otherwise you have the camera suddenly popping in and out from the player as it passes behind trees.

One way to deal with the chainlink fence issue it to simply flag its collider with a type that’s ignored by the camera, but sometimes you have world geometry where it’s annoying to have the camera pop in when it really shouldn’t (such as passing behind a thin tree, lamp post, etc.). Unless your tech artists are really on the ball, it’s really hard to ensure all colliders are properly flagged for the camera’s sake. Especially if they’re static colliders that’s part of the world mesh.

You can counteract this in a few steps:

  1. Do a fat ray test between the camera’s old and new positions. Make sure it’s clear of obstructions.

  2. Do a fat ray test between the camera’s new position and player. If it’s obstructed, start a timer.

  3. Over the next few frames, if the camera-player test is still blocked after a certain interval, then pop in towards the player.

EDIT: Oh yes, forgot to add: Don’t pop the camera back out! Let it slowly pull back from the player. I’ve found this gives a better user experience.

1 Like

Thank you.