I have a first person camera and from what I have learnt plus recommendations from others, I made my main camera separate from my player object. So I have an empty game object “camera holder” that contains my main camera, with its own camera script.
I manually type in an offset in the camera inspector of where I want my camera to be positioned during runtime. However, in my player movement script I have crouching code and when I crouch, the camera does not move with the player like it should because of the offset value. I have tried to reference the camera in my player movement script and use that to make the camera change its position during crouching but have not had success with that.
using UnityEngine;
public class FollowPlayer : MonoBehaviour
{
public Transform player;
public Vector3 offset;
public float mouseSensitivity = 100f;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
// Make the camera follow the player
transform.position = player.position + offset;
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.fixedDeltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.fixedDeltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.rotation = Quaternion.Euler(xRotation, player.localRotation.eulerAngles.y, 0f);
player.Rotate(Vector3.up * mouseX);
}
}
Is there a way to change the manual offset so that the camera changes position on its own when I’m doing movement like crouching or am I looking in the wrong spot in my code for a solution? I’ve had issues with this for some time.
I can’t really tell though because I’ve been told different things, is it OK for me to keep the main camera as a child of my player, or should I learn to keep them separated? Because having the camera as a child was a lot simpler and easier but not sure of the drawbacks
Minecraft has an FPS camera. Escape from Tarkov has an FPS camera. They are not even remotely similar.
Engineering is like this. This is software engineering. You won’t be told a response, you can only be told options.
Your task as a game engineer is to evaluate what you are doing, learn what the parts are doing and from that decide your next course of action based on the precise specific needs of the game you contemplate.
As always with engineering, look for the solution with the FEWEST moving parts. This is critical. One does not build an airplane simply by hammering bits of metal together until you’re happy. Each piece is there for a reason, and if the piece doesn’t need to be there, it won’t be.