Search Unity

Rotation Drag iOS Problem

Discussion in 'Scripting' started by amirivala, Oct 21, 2014.

  1. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    57
    0

    Hi Im new to coding and I'm using this code to rotate my object using touch input and its working perfectly using unity editor with mouse and when I'm build it and try it iOS the rotation works fine but when i try to rotate it again it jumps back to the first rotation variable and it looks like its jumped, can you help me to make it rotate perfectly

    sorry for my bad english

    really appreciate it

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class DragRotateSlowdown : MonoBehaviour {
    4.    
    5.      private float rotationSpeed = 10.0F;
    6.      private float lerpSpeed = 1.0F;
    7.    
    8.      private Vector3 theSpeed;
    9.      private Vector3 avgSpeed;
    10.      private bool isDragging = false;
    11.      private Vector3 targetSpeedX;
    12.    
    13.      void OnMouseDown() {
    14.        
    15.          isDragging = true;
    16.      }
    17.    
    18.      void Update() {
    19.        
    20.          if (Input.GetMouseButton(0) && isDragging) {
    21.              theSpeed = new Vector3(-Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0F);
    22.              avgSpeed = Vector3.Lerp(avgSpeed, theSpeed, Time.deltaTime * 5);
    23.          } else {
    24.              if (isDragging) {
    25.                  theSpeed = avgSpeed;
    26.                  isDragging = false;
    27.              }
    28.              float i = Time.deltaTime * lerpSpeed;
    29.              theSpeed = Vector3.Lerp(theSpeed, Vector3.zero, i);
    30.          }
    31.        
    32.          transform.Rotate(Camera.main.transform.up * theSpeed.x * rotationSpeed, Space.World);
    33.          transform.Rotate(Camera.main.transform.right * theSpeed.y * rotationSpeed, Space.World);
    34.      }
    35. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    What's happening is that you are currently simulating mouse input with touch. This works fine in many cases, but not this one. Unlike a real mouse, the only time that a touchscreen has a "mouse position" is when it's being touched (if the mouse position is moving, it must by definition have the "mouse button down"); so, when you lift your finger and move it, the touchscreen will see the movement from the last-touched place to the newly-touched place as a "move" on the first frame you touch it.

    Solutions:
    A) You can ignore input on the first frame after a touch, and that should fix it.
    B) A more proper way to fix it would be to change this function to use Input.touches instead of simulating mouse input.
     
    amirivala likes this.
  3. amirivala

    amirivala

    Joined:
    May 29, 2013
    Posts:
    57
    Thank You So much, really appreciate it