My game is first person and I’m using 3D with no cinemachine. I have a ceiling object that I can only pass through when crouched. Currently when I crouch, my camera will change its position to match my player scale when crouching, and when I uncrouch, nothing happens because I am under a ceiling, which is all good.
However, if I uncrouch whilst under a ceiling and then move out of the ceiling, my player will automatically revert back to standing scale (which is good) but the camera will still be stuck in the crouch position. To fix it, I just tap the crouch key and the camera reverts back to its original standing position. I am trying to make the camera move to default position just like the player scale reverts automatically.
My solution was to add a while loop but that froze my unity during runtime.
public class FollowPlayer : MonoBehaviour
{
public PlayerMovement move;
public Transform player;
private Vector3 offset = new Vector3(0f, 0.779f, 0f); // default camera position on runtime
private Vector3 newOffset = new Vector3(0f, -0.100f, 0f); // camera position while crouched
private Vector3 originalOffset = new Vector3(0f, 0.779f, 0f); // camera position after uncrouching
public void Update()
{
// Make the camera follow the player
transform.position = player.position + offset;
...
if (Input.GetKeyDown(KeyCode.LeftControl))
{
offset = newOffset;
}
if (Input.GetKeyUp(KeyCode.LeftControl))
{
offset = originalOffset;
}
if (Input.GetKeyUp(KeyCode.LeftControl) && move.isCeilingAbove == true)
{
offset = newOffset;
}
}
}
I have my isCeilingAbove bool referenced from another script which prevents my player from uncrouching whilst under an object using the “Ceiling” tag. This may not be the best way to do this but I want to get it right using my own script.