I have a 3D object that I want to rotate with mouse/finger swipe, so I made the script below.

The object's rotation looks smooth on editor, but when playing the game on real device (android), the rotation didn't follow the finger movement immediately, it takes some milliseconds to follow finger, it isn't smooth and the controls become hard and frustrating!

float sensitivity = 0.8f;
Vector2 firstPressPos;
Vector2 secondPressPos;

void Update()
{

    if (Input.GetMouseButtonDown(0))
    {
        //save began touch 2d point
        firstPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);
    }

    if (Input.GetMouseButton(0))
    {
        //save ended touch 2d point
        secondPressPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y);

        if (firstPressPos != secondPressPos)
        {
            float RotX = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
            float RotY = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime;
            transform.RotateAround(Vector3.up, RotX);
            transform.RotateAround(Vector3.right, -RotY);
        }
    }
}

2 Answers

2
Qusai Azzam On Best Solutions

try this code

// Screen Touches
    Vector2?[] oldTouchPositions = {
        null,
        null
    };
    // Rotation Speed
    public float rotSpeed = 0.5f;

    public void Update()
    {
        if (Input.touchCount == 0)
        {
            oldTouchPositions[0] = null;
            oldTouchPositions[1] = null;
        }
        else if (Input.touchCount == 1)
        {
            if (oldTouchPositions[0] == null || oldTouchPositions[1] != null)
            {
                oldTouchPositions[0] = Input.GetTouch(0).position;
                oldTouchPositions[1] = null;
            }
            else
            {
                Vector2 newTouchPosition = Input.GetTouch(0).position;
                float distanceX = (oldTouchPositions[0] - newTouchPosition).Value.x;
                float distanceY = (oldTouchPositions[0] - newTouchPosition).Value.y;

                float rotX = distanceX * rotSpeed * Mathf.Deg2Rad;
                float rotY = distanceY * rotSpeed * Mathf.Deg2Rad;


                transform.Rotate(Vector3.up, rotX * 5, Space.Self);
                transform.Rotate(Vector3.left, rotY * 5, Space.Self);


                oldTouchPositions[0] = newTouchPosition;

            }
        }
        else
        {
            if (oldTouchPositions[1] == null)
            {
                oldTouchPositions[0] = Input.GetTouch(0).position;
                oldTouchPositions[1] = Input.GetTouch(1).position;
            }
            else
            {

            }
        }
    }
0
ApolloSoftware On

Here's a script I found a few years back that manipulates X axis spin with Mouse or Touches. It seems to work well with touches, but not as good with Mouse. Try it and let me know if it behaves a bit better. This one should adjust the spinning speed dynamically:

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

public class SpinDrag : MonoBehaviour {

    float f_lastX = 0.0f;
    float f_difX = 0.5f;
    float f_steps = 0.0f;
    int i_direction = 1;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            f_difX = 0.0f;
        }
        else if (Input.GetMouseButton(0))
        {
            f_difX = Mathf.Abs(f_lastX - Input.GetAxis("Mouse X"));

            if (f_lastX < Input.GetAxis("Mouse X"))
            {
                i_direction = -1;
                transform.Rotate(Vector3.up, -f_difX);
            }

            if (f_lastX > Input.GetAxis("Mouse X"))
            {
                i_direction = 1;
                transform.Rotate(Vector3.up, f_difX);
            }

            f_lastX = -Input.GetAxis("Mouse X");
        }
        else
        {
            if (f_difX > 0.5f) f_difX -= 0.05f;
            if (f_difX < 0.5f) f_difX += 0.05f;

            transform.Rotate(Vector3.up, f_difX * i_direction);
        }
    }
}