WebGL can't get camera rotation to work

Hello

I’m working on a scene with a MainCamera. Attached to it I have a CameraRotatorScript:

using UnityEngine;

public class CameraRotator : MonoBehaviour
{
    public Transform target;
    public Camera mainCamera;

    [Range(0.1f, 5f)] [Tooltip("How sensitive the mouse drag to camera rotation")]
    public float mouseRotateSpeed = 0.8f;

    [Range(0.01f, 100)] [Tooltip("How sensitive the touch drag to camera rotation")]
    public float touchRotateSpeed = 17.5f;

    [Tooltip("Smaller positive value means smoother rotation, 1 means no smooth apply")]
    public float slerpValue = 0.25f;

    public enum RotateMethod
    {
        Mouse,
        Touch
    }

    [Tooltip("How do you like to rotate the camera")]
    public RotateMethod rotateMethod = RotateMethod.Mouse;

    private Vector2 swipeDirection; // swipe delta vector2
    private Quaternion cameraRot; // store the quaternion after the slerp operation
    private Touch touch;
    private float distanceBetweenCameraAndTarget;

    private readonly float minXRotAngle = -80; // min angle around x axis
    private readonly float maxXRotAngle = 80; // max angle around x axis

    //Mouse rotation related
    private float rotX; // around x
    private float rotY; // around y

    private void Awake()
    {
        if (mainCamera == null)
        {
            mainCamera = Camera.main;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        distanceBetweenCameraAndTarget = Vector3.Distance(mainCamera.transform.position, target.position);
    }

    // Update is called once per frame
    void Update()
    {
        switch (rotateMethod)
        {
            case RotateMethod.Mouse:
            {
                if (Input.GetMouseButton(0))
                {
                    rotX += -Input.GetAxis("Mouse Y") * mouseRotateSpeed; // around X
                    rotY += Input.GetAxis("Mouse X") * mouseRotateSpeed;
                }

                if (rotX < minXRotAngle)
                {
                    rotX = minXRotAngle;
                }
                else if (rotX > maxXRotAngle)
                {
                    rotX = maxXRotAngle;
                }

                break;
            }
            case RotateMethod.Touch:
            {
                if (Input.touchCount > 0)
                {
                    touch = Input.GetTouch(0);
                    switch (touch.phase)
                    {
                        case TouchPhase.Began:
                            //Debug.Log("Touch Began");
                            break;
                        case TouchPhase.Moved:
                            swipeDirection += touch.deltaPosition * Time.deltaTime * touchRotateSpeed;
                            break;
                        case TouchPhase.Ended:
                            //Debug.Log("Touch Ended");
                            break;
                    }
                }

                if (swipeDirection.y < minXRotAngle)
                {
                    swipeDirection.y = minXRotAngle;
                }
                else if (swipeDirection.y > maxXRotAngle)
                {
                    swipeDirection.y = maxXRotAngle;
                }

                break;
            }
        }
    }

    private void LateUpdate()
    {
        Vector3 dir = new Vector3(
            0,
            0,
            -distanceBetweenCameraAndTarget); // assign value to the distance between the maincamera and the target

        // value equal to the delta change of our mouse or touch position
        Quaternion newQ = rotateMethod == RotateMethod.Mouse
            ? Quaternion.Euler(rotX, rotY, 0) // We are setting the rotation around X, Y, Z axis respectively
            : Quaternion.Euler(swipeDirection.y, -swipeDirection.x, 0);

        cameraRot = Quaternion.Slerp(
            cameraRot,
            newQ,
            slerpValue); // let cameraRot value gradually reach newQ which corresponds to our touch
        mainCamera.transform.position = target.position + (cameraRot * dir);
        mainCamera.transform.LookAt(target.position);
    }

    public void SetCamPos()
    {
        if (mainCamera == null)
        {
            mainCamera = Camera.main;
        }

        mainCamera.transform.position = new Vector3(
            0,
            0,
            -distanceBetweenCameraAndTarget);
    }
}

This works in the editor so far. But as soon, building the WebGL output, the camera can not be rotated anymore.
Does some of you have an idea what I’m missing?

Kind regards

Add Debug.Log statements everywhere to figure out what is happening. Your post doesn’t give many clues.

I suggest you avoid having completely different codepaths for touch and mouse… that will be hard to maintain.

You’re welcome to look at how I wrapped it up in my MicroTouch class over in my proximity_buttons project. As long as you use MicroTouch, then “It Just Works™” on any platform.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

1 Like

Thank you. I have figured it out, there was another camera attached that blocked the behavior.

1 Like

Ok. Thank you for your suggestion. I will try to eliminate the additional behavior for touch.