Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[Solved] Rotate sphere on mobile with slide movement

Discussion in 'Scripting' started by Mazdamundi, Jan 14, 2019.

  1. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    9
    Hi everyone !

    So I have a little problem and I need your help !!

    I have to rotate my object with a slide movement with my finger. On my pc it's working well (the movement is very fluid) but on mobile I have a little problem.
    Here the script :

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RotatePortals: MonoBehaviour
    6. {
    7.    public Transform target;
    8.    public float distance = 2.0f;
    9.    public float xSpeed = 20.0f;
    10.    public float ySpeed = 20.0f;
    11.    public float yMinLimit = -90f;
    12.    public float yMaxLimit = 90f;
    13.    public float xMinLimit = -90f;
    14.    public float xMaxLimit = 90f;
    15.    public float distanceMin = 10f;
    16.    public float distanceMax = 10f;
    17.    public float smoothTime = 2f;
    18.    float rotationYAxis = 0.0f;
    19.    float rotationXAxis = 0.0f;
    20.    float velocityX = 0.0f;
    21.    float velocityY = 0.0f;
    22.  
    23.    private Vector3 _mouseReference;
    24.    private Vector3 _mouseOffset;
    25.  
    26.    // Use this for initialization
    27.    void Start()
    28.    {
    29.        Vector3 angles = transform.eulerAngles;
    30.        rotationYAxis = angles.y;
    31.        rotationXAxis = angles.x;
    32.        // Make the rigid body not change rotation
    33.        if (GetComponent<Rigidbody>())
    34.        {
    35.            GetComponent<Rigidbody>().freezeRotation = true;
    36.        }
    37.    }
    38.    void LateUpdate()
    39.    {
    40.  
    41.        if (target)
    42.        {
    43.            _mouseOffset = (Input.mousePosition - _mouseReference);
    44.  
    45.            if (Input.GetMouseButton(0))
    46.            {
    47.                velocityX += xSpeed * (_mouseOffset.x + _mouseOffset.y) * distance * 0.02f;
    48.                velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
    49.            }
    50.            rotationYAxis += -velocityX;
    51.            rotationXAxis -= -velocityY;
    52.            rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
    53.            rotationYAxis = ClampAngle(rotationYAxis, xMinLimit, xMaxLimit);
    54.            Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
    55.            Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
    56.            Quaternion rotation = toRotation;
    57.  
    58.            //distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
    59.            //RaycastHit hit;
    60.            //if (Physics.Linecast(target.position, transform.position, out hit))
    61.            //{
    62.            //distance -= hit.distance;
    63.            //}
    64.            //Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
    65.            //Vector3 position = rotation * negDistance + target.position;
    66.  
    67.            transform.rotation = rotation;
    68.            _mouseReference = Input.mousePosition;
    69.            //transform.position = position;
    70.            velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
    71.            velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
    72.  
    73.        }
    74.    }
    75.    public static float ClampAngle(float angle, float min, float max)
    76.    {
    77.        if (angle < -360F)
    78.            angle += 360F;
    79.        if (angle > 360F)
    80.            angle -= 360F;
    81.        return Mathf.Clamp(angle, min, max);
    82.    }
    83.  
    84.    void OnMouseDown()
    85.    {
    86.  
    87.        // store mouse position
    88.        _mouseReference = Input.mousePosition;
    89.    }
    90. }
    91.  
    92.  
    The problem is that when I click on my screen the object rotate. It's like when I click on X = 150 the object rotate for have Rotation X = 150. On the PC it's not a problem, the object rotate only when I do a slide movement.

    So I found a script who works well but it's not the good movement (not fluid, not smooth), and I can't mix them together for have what I want !!
    Here this script :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Rotate_Test : MonoBehaviour {
    7.  
    8.     #region ROTATE
    9.     private float _sensitivity = 1f;
    10.     private Vector3 _mouseReference;
    11.     private Vector3 _mouseOffset;
    12.     private Vector3 _rotation = Vector3.zero;
    13.     private bool _isRotating;
    14.  
    15.     #endregion
    16.  
    17.     void Update()
    18.     {
    19.         if(_isRotating)
    20.         {
    21.             // offset
    22.             _mouseOffset = (Input.mousePosition - _mouseReference);
    23.             // apply rotation
    24.             _rotation.y = -(_mouseOffset.x + _mouseOffset.y) * _sensitivity;
    25.             // rotate
    26.             gameObject.transform.Rotate(_rotation);
    27.             // store new mouse position
    28.             _mouseReference = Input.mousePosition;
    29.         }
    30.     }
    31.     void OnMouseDown()
    32.     {
    33.         // rotating flag
    34.         _isRotating = true;
    35.         // store mouse position
    36.         _mouseReference = Input.mousePosition;
    37.     }
    38.     void OnMouseUp()
    39.     {
    40.         // rotating flag
    41.         _isRotating = false;
    42.     }
    43. }
    Someone had an idea for mix them ? That's will be great !!

    Have a nice day !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    Insert at line 43 in the first script something like so:

    Code (csharp):
    1. if (Input.GetMouseButtonDown(0))  // only fires on the frame that the button goes DOWM
    2. {
    3.   // captures the current reference so it is "zeroed out"
    4.   _mouseReference = Input.mousePosition;
    5. }
    Just be sure that happens BEFORE you calculate _mouseOffset.
     
  3. Mazdamundi

    Mazdamundi

    Joined:
    Dec 5, 2017
    Posts:
    9
    Ho, thanks a lot !! It's working well :).

    Thanks again !!
     
    Kurt-Dekker likes this.