• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by sanja15513 · Nov 05, 2018 at 02:59 PM · touchrotatecube

Rotate object with touch

Does anyone know how to rotate an object with touch. For example rotating a cube from all sides, but not using onMouseDrag and using touch input instead?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by AaronXRDev · Nov 05, 2018 at 03:29 PM

@sanja15513 Here's a quick script I use on update on a GameObject to rotate it along the x axis. It could be modified to rotate along multiple axises by adding the deltaPosition.y

         if(Input.touchCount == 1)
         {
             float rotateSpeed = 0.09f;
             Touch touchZero = Input.GetTouch(0);
 
             //Rotate the model based on offset
             Vector3 localAngle = activeARfurniture.transform.localEulerAngles;
             localAngle.y -= rotateSpeed * touchZero.deltaPosition.x;
             activeARfurniture.transform.localEulerAngles = localAngle;
         }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image azizkale · Jun 10, 2019 at 07:46 AM 1
Share

the codes are great. but could you tell also for rotating on another axises? because i tried to rotate on x axis but it rotated on 180 degre

avatar image nanditho · Mar 12 at 05:43 AM 0
Share

How is it possible to rotate object along its all axis on finger swipe / drag gesture regarding the camera pan / rotation?

Thanks

avatar image
1

Answer by kskjadav007 · Feb 05, 2020 at 09:15 AM

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class Functionalities : MonoBehaviour
 {
 
     public GameObject m_objecttorotate;
 
     [Space]
     public int m_minScale;
     public int m_maxScale;
 
     private float initialFingersDistance;
 
     private Vector3 initialScale;
     private float m_firstpoint;
     private float m_secondpoint;
 
     private int m_inc = 0;
 
     void Update()
     {
 
         if (Input.touchCount == 0)
         {
             m_inc = 0;
             return;
         }
 
 
         if (m_objecttorotate==null)
         {
             return;
         }
 
 
         if (Input.touchCount == 1)
         {  
             if (m_inc==0)
             {
                 m_firstpoint = (int)Input.GetTouch(0).position.x;
                 m_secondpoint = (int)Input.GetTouch(0).position.x;
             }
 
             m_inc++;
 
             if (m_inc<=10)
             {
                 return;
             }
 
             m_secondpoint = (int)Input.GetTouch(0).position.x;
 
             if (m_firstpoint<m_secondpoint)
             {
                 _Rotating(false);
             }
             else if (m_firstpoint > m_secondpoint)
             {
                 _Rotating(true);
             }
 
 
             return;
         }
 
         if (Input.touches.Length == 2)
         {
             _Scaling();
             return;
         }
     }
 
 
     private void LateUpdate()
     {
         if (m_inc>=10)
         {
             m_firstpoint = (int)Input.GetTouch(0).position.x;
         }
     }
 
     void _Rotating(bool m_right)
     {
 
         //Debug.Log(m_right);
 
         if (m_right)
         {
             m_objecttorotate.transform.Rotate(Vector3.up * Time.deltaTime *200f);
         }
         else
         {
             m_objecttorotate.transform.Rotate(Vector3.down * Time.deltaTime *200f);
         }
     }
 
 
     void _Scaling()
     {
         if (Input.touches.Length == 2)
         {
             Touch t1 = Input.touches[0];
             Touch t2 = Input.touches[1];
 
             if (t1.phase == TouchPhase.Began || t2.phase == TouchPhase.Began)
             {
                 initialFingersDistance = Vector2.Distance(t1.position, t2.position);
                 initialScale = m_objecttorotate.transform.localScale;
             }
             else if (t1.phase == TouchPhase.Moved || t2.phase == TouchPhase.Moved)
             {
 
                 float currentFingersDistance = Vector2.Distance(t1.position, t2.position);
                 var scaleFactor = currentFingersDistance / initialFingersDistance;
 
                 Vector3 m_scale= initialScale * scaleFactor;
 
                 m_scale.x = Mathf.Clamp(m_scale.x, m_minScale,m_maxScale);
                 m_scale.y = Mathf.Clamp(m_scale.y, m_minScale, m_maxScale);
                 m_scale.z = Mathf.Clamp(m_scale.z, m_minScale, m_maxScale);
                 m_objecttorotate.transform.localScale = m_scale;
                 m_detailstext.text = m_scale.ToString();
             }
         }
 
     }
 }


this is what i made for rotation and scaling object with pintch.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chaitanyajadhav · Feb 05, 2020 at 11:43 AM 0
Share

Tried with your script but it is not working. Neither rotation nor Scale is working with this.

avatar image kskjadav007 · Feb 06, 2020 at 04:55 AM 0
Share

You need to put an object which you want to rotate in Inspector .

and this script only works with mobile devices.

avatar image Bhanuteja_g · Nov 24, 2020 at 03:28 PM 1
Share

Thanks @kskjadav007.

This script is working. I am able to successfully rotate the object along y axis and scale it up also.

Can you tell me how to rotate the object in multiple axes? I think I have to use Input.GetTouch(1) with the same logic for doing this??

avatar image aloysiusw · Feb 10, 2021 at 09:29 PM 0
Share

I tried the script and it works great, but I would like the object to scale uniformly. It is currently scaling the object on all axes making the object look like an accordion. I feel like I need to change something in lines 121-124, but not sure to what exactly. This is one of the only threads I've found on both a rotate and scale touches, so I was hoping you would be willing to explain what I bet is a simple solution.

avatar image
0

Answer by chaitanyajadhav · Feb 05, 2020 at 07:47 AM

Thanks for the script @EgoAnt It works perfect except the rotation is not complete 360 degree around x and y axis which is what i was looking for. Please tell me how to do complete 360 degree rotation. It will be really helpful.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Llama_w_2Ls · Nov 24, 2020 at 04:17 PM

You could use two quaternion rotations (for x and y axis) and multiply them together to get a smooth transition. The following script was made in WPF but can be modified for touch input on an object in Unity.

         Vector2 InitialMouseTouch = new Vector2();
 
         Quaternion PreviousQuatX = new Quaternion();
         Quaternion PreviousQuatY = new Quaternion();
         Quaternion QuatX = new Quaternion();
         Quaternion QuatY = new Quaternion();
 
         void Update()
         {
             float ScreenWidth = 1920;
             float ScreenHeight = 1080;
 
             Vector2 CurrentMouseTouch = Input.MousePosition; // PSEUDOCODE
 
             if (Input.MouseButtonDown(0))
             {
                 InitialMouseTouch = Input.MousePosition;
                 PreviousQuatX = QuatX;
                 PreviousQuatY = QuatY;
             }
 
             double RotY = (CurrentMouseTouch.X - InitialMouseTouch.X) / ScreenWidth * LookSensitivity;
             double RotX = (CurrentMouseTouch.Y - InitialMouseTouch.Y) / ScreenHeight * LookSensitivity;
 
             QuatX = Quaternion.Multiply(new Quaternion(new Vector3D(1, 0, 0), -RotX), PreviousQuatX);
             QuatY = Quaternion.Multiply(new Quaternion(new Vector3D(0, 1, 0), -RotY), PreviousQuatY);
             Quaternion QuaternionRotation = Quaternion.Multiply(QuatY, QuatX); 
             // Composite Quaternion between the x rotation and the y rotation
 
             // Apply the QuaternionRotation quaternion onto an object to rotate it.
         }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nanditho · Mar 12 at 05:44 AM

How is it possible to rotate object along its all axis on finger swipe / drag gesture regarding the camera pan / rotation?

Thanks

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate a Cube by 90 degree 0 Answers

How to drag two Cubes by Touch At the same time? 0 Answers

rotate object to touch position 1 Answer

rotate a texture inside a cube 1 Answer

Cannot get GUIUtility.RotateAroundPivot working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges