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

Rotate game object on swipe

Discussion in 'Scripting' started by gargoylesoft, May 6, 2015.

  1. gargoylesoft

    gargoylesoft

    Joined:
    Apr 27, 2015
    Posts:
    1
    I've got a game object in my scene that when I move my finger across the screen, I want the object to rotate in that direction. It's a cube on screen, and sliding my finger around on the screen should rotate the cube about the center point, but not move the cube's position. It should only rotate as long as they are actively swiping.

    I'm basically trying to replicate 3D minesweeper.

    I'm very familiar with C# development but I have almost no experience with unity or 3d development.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  3. BYELIK

    BYELIK

    Joined:
    Mar 25, 2012
    Posts:
    15
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.EventSystems;
    4.  
    5. public class CharacterRotating : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     private Camera cam;
    9.  
    10.     [SerializeField]
    11.     private Quaternion defaultAvatarRotation;
    12.  
    13.     [SerializeField]
    14.     private float slowSpeedRotation = 0.03f;
    15.     [SerializeField]
    16.     private float speedRotation = 0.03f;
    17.  
    18.     private const string AVATAR_TAG = "avatar_model";
    19.  
    20.     private bool isRotating = false;
    21.  
    22.     private RaycastHit hit;
    23.  
    24.     void Awake()
    25.     {
    26.         Camera[] cameraList = FindObjectsOfType(typeof(Camera)) as Camera[];
    27.         foreach (Camera camObj in cameraList)
    28.         {
    29.             if(camObj && camObj.tag == "Untagged")
    30.             {
    31.                 cam = camObj;
    32.             }
    33.         }
    34.         defaultAvatarRotation.y = 180.0f;
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.         MouseButtonDown();
    41.         MouseButotnUp();
    42.         if (Input.GetMouseButton(0) && isRotating)
    43.         {
    44.             RaycastHit dragingHit;
    45.  
    46. #if UNITY_EDITOR
    47.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    48. #elif UNITY_ANDROID
    49.  
    50.             Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
    51. #endif
    52.             if(Physics.Raycast(ray, out dragingHit) && dragingHit.collider.gameObject == hit.collider.gameObject)
    53.             {
    54.                 if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
    55.                 {
    56.  
    57. #if UNITY_EDITOR
    58.                     float x = -Input.GetAxis("Mouse X");
    59. #elif UNITY_ANDROID
    60.  
    61.                     float x = -Input.touches[0].deltaPosition.x;
    62. #endif
    63.                     transform.rotation *= Quaternion.AngleAxis(x * speedRotation, Vector3.up);
    64.                 }
    65.             }
    66.         }
    67.         else
    68.         {
    69.             if(transform.rotation.y != defaultAvatarRotation.y)
    70.             {
    71.                 SlowRotation();
    72.             }
    73.         }
    74.     }
    75.  
    76.     private void MouseButtonDown()
    77.     {
    78.         if(Input.GetMouseButtonDown(0))
    79.         {
    80.  
    81. #if UNITY_EDITOR
    82.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    83. #elif UNITY_ANDROID
    84.         Ray ray = cam.ScreenPointToRay(Input.touches[0].position);
    85. #endif
    86.             if(Physics.Raycast(ray, out hit))
    87.             {
    88.                 if(hit.collider.tag == AVATAR_TAG && transform == hit.collider.transform)
    89.                 {
    90.                     isRotating = true;
    91.                 }
    92.             }
    93.         }
    94.     }
    95.  
    96.     private void MouseButotnUp()
    97.     {
    98.         if(Input.GetMouseButtonUp(0))
    99.         {
    100.             isRotating = false;
    101.             hit = new RaycastHit();
    102.         }
    103.     }
    104.  
    105.     private void SlowRotation()
    106.     {
    107.         transform.rotation = Quaternion.Slerp(transform.rotation,
    108.                                               defaultAvatarRotation,
    109.                                               slowSpeedRotation * Time.deltaTime);
    110.     }
    111. }
    If you need I can make a small test project for more understanding of this code.
     
    U7Games likes this.
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Simple way:
    Code (CSharp):
    1.     public Transform cube;
    2.  
    3.     void Update()
    4.     {
    5.         if (Input.touchCount == 1)
    6.         {
    7.             // GET TOUCH 0
    8.             Touch touch0 = Input.GetTouch(0);
    9.  
    10.             // APPLY ROTATION
    11.             if (touch0.phase == TouchPhase.Moved)
    12.             {
    13.                 cube.transform.Rotate(0f, touch0.deltaPosition.x, 0f);
    14.             }
    15.  
    16.         }
    17.     }
     
  5. mrosenschein

    mrosenschein

    Joined:
    Apr 16, 2017
    Posts:
    3
    Hey Byelik, I know this was posted a while ago but I'm interested in doing the same thing, is your offer to make a sample project still on the table? ;)
     
  6. DAKSHDD

    DAKSHDD

    Joined:
    Apr 4, 2018
    Posts:
    2
    tha
    nks bro
     
    yokeshram likes this.
  7. Ritwik_Sinha

    Ritwik_Sinha

    Joined:
    Apr 30, 2018
    Posts:
    3
    Code (CSharp):
    1. private Vector2 startPos,direction;
    2.     private float multiplierY=0.02f;
    3.     private float multiplierX=0.02f;
    4.  
    5.     void Awake()
    6.     {
    7.         instance=this;
    8.     }
    9.     void Update()
    10.     {
    11.         if(Input.touchCount>0)
    12.         {
    13.             Touch touch = Input.GetTouch(0);
    14.  
    15.             switch (touch.phase)
    16.             {
    17.                 case TouchPhase.Began:
    18.                     startPos = touch.position;
    19.                     direction=Vector3.zero;
    20.                     break;
    21.  
    22.                 case TouchPhase.Moved:
    23.                     direction = touch.position - startPos;
    24.                     if(direction.magnitude>0.05f)
    25.                     {
    26.                         // var localAngles=this.transform.localEulerAngles;
    27.                         // localAngles.y+=direction.y*multiplierY;
    28.                         // this.transform.localEulerAngles=localAngles;
    29.      
    30.                         var globalAngles=this.transform.eulerAngles;
    31.                         globalAngles.y+=-direction.x*multiplierX;
    32.                         this.transform.eulerAngles=globalAngles;
    33.  
    34.                         var dotVal1= Vector3.Dot(Camera.main.transform.forward,this.transform.right);
    35.                         var dotVal2= Vector3.Dot(Camera.main.transform.forward,this.transform.up);
    36.                         var dotVal3= Vector3.Dot(Camera.main.transform.right,this.transform.up);
    37.                      
    38.  
    39.                         // Debug.Log("Value"+(dotVal>0?true:false));
    40.                         // MyDebug.Log("Value"+(dotVal>0?true:false));
    41.  
    42.                         this.transform.localRotation*=Quaternion.Euler(0,-direction.y*multiplierY*(dotVal1>0?-1:1)*(dotVal1>0?-1:1)*(dotVal3>0?-1:1),0);
    43.                     }
    44.                     break;
    45.  
    46.                 case TouchPhase.Ended:
    47.                 case TouchPhase.Canceled:
    48.                     break;
    49.             }
    50.         }
    51.     }
    Try out this. It will work properly just like swipe to rotate on phones. Just drag this object on the gameobject you want to rotate using swipe
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Here's my contribution from a few years back, full package, scripts, scenes, setup, etc.
     

    Attached Files: