Unity

[Unity] Mouse Look Script

Guk-blog 2020. 3. 16. 16:21
728x90
반응형
public float mouseSensitivity = 100f;
    public Transform playerBody;
    float xRotation = 0f;

    void Start()
    {
      Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);// -90~90도까지만 Y축 움직임을 제한함

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX); // 마우스 x축 움직임에 따라 플레이어도 회전

    }

출처 : https://www.youtube.com/watch?v=_QajrabyTJc

728x90
반응형