Search code examples
c#unity-game-enginetouch

Main camera rotation by swipe


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour
{

    public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    public RotationAxes axes = RotationAxes.MouseXAndY;
    public float sensitivityX = 15F;
    public float sensitivityY = 15F;

    public float minimumX = -360F;
    public float maximumX = 360F;

    public float minimumY = -60F;
    public float maximumY = 60F;

    float rotationY = 0F;

    void Update()
    {
        if (axes == RotationAxes.MouseXAndY)
        {
            float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
        }
        else if (axes == RotationAxes.MouseX)
        {
            transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
        }
        else
        {
            rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
            rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

            transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
        }
    }

    void Start()
    {
        //if(!networkView.isMine)
        //enabled = false;

        // Make the rigid body not change rotation
        //if (rigidbody)
        //rigidbody.freezeRotation = true;
    }
}

I use a script for rotation of the camera with the mouse and I want to transform it for the touch . I want my camera to rotate just when I touch the screen . The game has a first player controller . Please help me to make it for touchscreen


Solution

  • Get the first Touch, then use touch.deltaPosition to find how much the touch has moved since the last update. Then, you can scale with screen size and sensitivity.

    void Update()
    {
        if(Input.touchCount > 0) {
            Touch touch = Input.GetTouch(0);
            float turnAngleChange = (touch.deltaPosition.x / Screen.width) * sensitivityX; 
            float pitchAngleChange = (touch.deltaPosition.y / Screen.height) * sensitivityY;
    
            // Handle any pitch rotation
            if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseY) {
                rotationY = Mathf.Clamp(rotationY+pitchAngleChange, minimumY, maximumY);
                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0f));
            }
    
            // Handle any turn rotation
            if (axes == RotationAxes.MouseXAndY || axes == RotationAxes.MouseX) {
                transform.Rotate(0f, turnAngleChange , 0f);
            }
        }
    }
    

    As a sidenote, it can be confusing to call pitch rotations rotationY even though they are actually rotations along the x axis, and vice versa.