My camera does not follow my player when player scale is changed?

My project is 3D and first person and I do not keep my player object and main camera together and so I have Player.cs and Camera.cs scripts. The way I have created it might be poorly done but for the moment I am trying to fix a problem that has come up.

In my Player.cs script, I have code such as laying down/proning and also crouching, and the script has no references to my camera.

But when I do either of those two movements, my camera remains at the default player standing height. To fix this, I added different camera position offsets in my Camera.cs script below. But because in my Player script I have a bool to check for a ceiling, I can’t get it to detect the same thing in my camera script so now when I crouch under a ceiling, my player does not stand up (which is good), but my camera goes up to default player standing height which in this case, is in the ceiling.

// Just my offset code displayed
public class Camera : MonoBehaviour
{

    public Transform playerChar;
    private Vector3 defaultOffset = new Vector3(0, 0.779f, 0);
    private Vector3 newOffset = new Vector3(0, -0.100f, 0);
    private Vector3 originalOffset = new Vector3(0, 0.779f, 0);

    ... 

    void Update()
    {

        transform.position = playerChar.position + defaultOffset;

        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            defaultOffset = newOffset;
        }

        if (Input.GetKeyUp(KeyCode.LeftControl))
        {
            defaultOffset = originalOffset;
        }
    }
}

Am I able to reference the bool from the Player script in my Camera script, if that is the right approach? Unless I need to completely rethink how I am approaching this.

Without having seen your Player code, crouching and similar actions usually dont change the player position, so it is to be expected that the camera, which is based on the player position, is unaffected.

Generally speaking, yes. It would make sense to reference your Player script instead of just the Transform, which allows you to access public properties and variables (or even methods) of your player and use them to optimize your camera.

As you mentioned in the start, your current approach is very rudimentary. Experimenting around like that is fine, and you seem to have reached a reasonable conclusion. However, for any given topic i would advice to first look up some guides or articles on how others implemented them. Especially if it is something so commonplace as character movement or camera handling - which the majority of games need to solve. You are never the first to have a problem, and getting an idea of what you have to deal with and how others solved it will usually give you a pretty good idea how to start your own solution(s).

Last but not least, for your character you may want to look into state machines aswell. This is its own topic, but prevents you from having douzens of bools like isCrouching, isSwimming, isGliding, … and instead base the character movement and actions/options on its current state. You could then also use this state in your camera to calculate the correct offset.

1 Like

Don’t name your class Camera. That’s just asking for future mysterious errors.

In general, camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.

There’s even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/