0

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);
    }
}
Uzair Ashraf
  • 1,171
  • 8
  • 20

1 Answers1

0

Right after you call that SetCameraStartingPosition method, 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 used CursorLockMode.Locked right 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.

SaturnusK1
  • 46
  • 1
  • 8
  • Switched them around, that wasn't it. – Uzair Ashraf Jul 07 '20 at 04:39
  • Switching them won't fix the problem. You update the transform camera based on cursor position in the window. So you should either set cursor position (instead of camera transform) to camera starting position in ```SetCameraStartingPosition``` or use anything other than ```Input.GetAxis```. – SaturnusK1 Jul 08 '20 at 07:10