PC to iOS woes... rotating sphere

I’ve got a script that is component of a stationary sphere that is used to rotate the sphere by clicking and dragging. It works flawlessly in the editor… when you click on the object and move the mouse, the sphere rotates in the direction you dragged and eventually slows to a stop. But I’m trying to move it to iPad, and now when you touch the object, it spins suddenly to a new orientation before it begins moving. Can someone take a look at this and see what might be causing this weird jump before the rotation begins?

    var rotationSpeed = 10.0;
    var lerpSpeed = 1.0;
     
    private var speed = new Vector3();
    private var avgSpeed = new Vector3();
    private var dragging = false;
    private var targetSpeedX = new Vector3();
     
    function OnMouseDown()
    {
       dragging = true;
    }
     
    function Update ()
    {
     
       if (Input.GetMouseButton(0)  dragging) {
          speed = new Vector3(-Input.GetAxis ("Mouse X"), Input.GetAxis("Mouse Y"), 0);
          avgSpeed = Vector3.Lerp(avgSpeed,speed,Time.deltaTime * 5);
       } else {
       if (dragging) {
          speed = avgSpeed;
          dragging = false;
       }
       var i = Time.deltaTime * lerpSpeed;
       speed = Vector3.Lerp( speed, Vector3.zero, i);
       }
     
    transform.Rotate( Camera.main.transform.up * speed.x * rotationSpeed, Space.World );
    transform.Rotate( Camera.main.transform.right * speed.y * rotationSpeed, Space.World );
    }

One thing… I have tried changing Input.GetMouseButton(0) to Input.touchCount == 1, as I’ve read that’s more appropriate for iOS, but nothing really changed.

On touch screen devices mouse movement is emulated because there’s no actual mouse. One of the differences between a mouse and a touch is that a mouse always has a current set of coordinates, where a touch only has coordinates when the finger is on the screen.

The issue is that when emulating a mouse via touch, if the finger is lifted there is no valid mouse coordinate, so that stops being updated. When you put your finger down next it’s as if the mouse was instantly moved from the old position to the new position. If your drag detection code doesn’t account for this (you need to ignore any “drag” movement for the first frame of any new click) you’ll have exactly the issue you described, as your code will treat any new tap as an instant drag from the old position to the new one due to how the mouse emulation works.

Wow. Thanks, angrypenguin I see the fundamental difference now. I’ll give this a shot.

Excellent! I put a simple if statement around the contents of the Update function checking to see if it’s the first frame after the Mousedown. Works great. For some reason, when I spin the sphere, then tap it to stop, it continues spinning when I lift up my finger. Still have to figure that out. But the weird jump problem is solved.