- Home /
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?
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;
}
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
How is it possible to rotate object along its all axis on finger swipe / drag gesture regarding the camera pan / rotation?
Thanks
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.
Tried with your script but it is not working. Neither rotation nor Scale is working with this.
You need to put an object which you want to rotate in Inspector .
and this script only works with mobile devices.
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??
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.
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.
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.
}
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
Your answer
Follow this Question
Related Questions
camera rotate with limitation around x and y axis by using touch 0 Answers
Rotating 2D Sprite on 1 Axis Using 2 Input Axes 0 Answers
touch rotate 1 Answer
How to scale and move a cuboid so that it fit's between two points? 3 Answers
How do I rotate an object on one axis to face android touch? 0 Answers