Somewhat new to Unity and C#, trying to let the camera move around for the user to see the room. It's kind of like a visual novel so I only want a specific part of the room to be visible.
This is what I have so far. It works perfectly but it starts at the minimum angle, and I want it to start at coordinates that I have set in the inspector.
I tried creating a method that will start it in the exact values, but that didn't work
public class CameraMovement : MonoBehaviour
{
// Start is called before the first frame update
public float mouseSensitivity = 70f;
public float yawMax = 90f;
public float yawMin = -90f;
public float pitchMax = 90f;
public float pitchMin = -90f;
private float yaw = 0.0f;
private float pitch = 0.0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
SetCameraStartingPosition();
}
// Update is called once per frame
void Update()
{
HandleMouseMovement();
}
void SetCameraStartingPosition() {
transform.eulerAngles = new Vector3(0.02f, 0.292f, -0.323f);
}
void HandleMouseMovement() {
yaw += mouseSensitivity * Input.GetAxis("Mouse X") * Time.deltaTime;
pitch -= mouseSensitivity * Input.GetAxis("Mouse Y") * Time.deltaTime;
yaw = Mathf.Clamp(yaw, yawMin, yawMax);
pitch = Mathf.Clamp(pitch, pitchMin, pitchMax);
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
}
}
Right after you call that
SetCameraStartingPositionmethod, the Update method will be called and it uses the cursor position to change the Camera rotation (or the transform that Camera attached to). But you usedCursorLockMode.Lockedright before that and the current cursor position is at the center of the window. So, before the first frame is even started to be shown your camera would go to the center of the window.