- Home /
Quaternions, rotating object on Y axis.
Objective: I would like to be able to select an object and use two finger touch/drag to rotate the object on the worlds Y axis.
For handling the mobile multitouch I am using the TouchScript assets. I attach the following to an empty game object, then nest my object within that.
using System;
using TouchScript.Events;
using TouchScript.Gestures;
using UnityEngine;
public class Cubes : MonoBehaviour {
private enum CubesState
{
Idle,
Rotating
}
public float AutoRotationSpeed = 10f;
public float RotationSpeed = 20f;
private CubesState State;
private Quaternion targetRotation;
public void Rotate(Vector3 axis)
{
if (State != CubesState.Idle) return;
State = CubesState.Rotating;
///targetRotation = Quaternion.AngleAxis (279, axis) * transform.localRotation;
///targetRotation = Quaternion.AngleAxis (270, transform.up) * transform.localRotation;
targetRotation = Quaternion.AngleAxis (45, transform.up) * transform.localRotation;
}
private void Start()
{
targetRotation = transform.localRotation;
GetComponent<RotateGesture>().StateChanged += onRotateStateChanged;
}
private void Update()
{
if (State == CubesState.Rotating)
{
var fraction = AutoRotationSpeed * Time.deltaTime;
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, fraction);
if (Quaternion.Angle(transform.localRotation, targetRotation) < .1)
{
transform.localRotation = targetRotation;
State = CubesState.Idle;
}
} else
{
var fraction = RotationSpeed * Time.deltaTime;
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, fraction);
}
}
private void onRotateStateChanged(object sender, GestureStateChangeEventArgs e)
{
if (State != CubesState.Idle) return;
switch (e.State)
{
case Gesture.GestureState.Began:
case Gesture.GestureState.Changed:
var gesture = (RotateGesture)sender;
if (Math.Abs(gesture.LocalDeltaRotation) > 0.01)
{
targetRotation = Quaternion.AngleAxis(gesture.LocalDeltaRotation, gesture.WorldTransformPlane.normal) * targetRotation;
}
break;
}
}
}
I've been trying various methods all day. From using the DragRigidBody.cs script provided by tiz777 (here, which works however it doesn't really do multitouch rotation, and it's not ideal. Although the raycasting to select objects is nice.
What is the best method to limit the rotation to the Y Axis?
Or am I approaching this totally wrong?
Answer by whydoidoit · Feb 22, 2014 at 12:39 AM
In onRotate etc you probably want to make:
targetRotation = Quaternion.AngleAxis(gesture.LocalDeltaRotation, Vector3.up) * targetRotation;
Your answer
![](https://koobas.hobune.stream/wayback/20220613134308im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change rotation of an object based on an HTC Vive Controller 1 Answer
Choose starting point/angle for rotation with cos and sin 0 Answers
What Should i use rigidbody.AddTorque() or Quaternions ? 1 Answer
Problem with Quaternion.LookRotation() 1 Answer
How to rotate an object depending on the rotation of an other Object 3 Answers