- Home /
Problem in Rotating Cube on swipe, using FingureGesture Swipes?
Hi !
I am using Cube as a 3D Menu which will have 6 different missions on each side, I have used Fingure Gestures plugin and its works fine in most of the cases e-g tap , drag , move , but in swipe when I get the SwipeDirection.direction it returns a direction which is not a real direction when I test on device e-g if (direction is Up) it should rotate cube in up direction , but moves in left direction which is weird.
I need help in understanding how Fingure Gesture swipe direction can be used to rotate a cube in proper direction where we swipes.
Please look at the following code: You can test it even on just adding these 2 scripts on a UnityCube, but settings its pivot at right direction.This script is used to control the swipes attach this C# script to Cube.
using UnityEngine;
using System.Collections;
public class MenuController : MonoBehaviour {
Vector3 heading;
bool Pinching;
float fov;
float minFov = 40f;
float maxFov = 100f;
float rotateFrom;
float rotateTo;
float sensitivity= 0.02f;
// variables for cube rotation control
public Quaternion targetRotation;
void Update()
{
}
void OnEnable () {
print ("Script is enabled");
FingerGestures.OnFingerSwipe += FingerGestures_OnFingerSwipe;
FingerGestures.OnPinchBegin += FingerGestures_OnPinchBegin;
FingerGestures.OnPinchMove += FingerGestures_OnPinchMove;
FingerGestures.OnPinchEnd += FingerGestures_OnPinchEnd;
}
// Convert from screen-space coordinates to world-space coordinates on the Z = 0 plane
public static Vector3 GetWorldPos( Vector2 screenPos )
{
Ray ray = Camera.main.ScreenPointToRay( screenPos );
// we solve for intersection with z = 0 plane
float t = -ray.origin.z / ray.direction.z;
return ray.GetPoint( t );
}
void FingerGestures_OnFingerSwipe( int fingerIndex, Vector2 startPos, FingerGestures.SwipeDirection direction, float velocity )
{
if( direction == FingerGestures.SwipeDirection.Up )
{
heading = Vector3.left;
//heading = Vector3.up;
}
else if( direction == FingerGestures.SwipeDirection.Down )
{
heading = Vector3.right;
//heading = Vector3.down;
}
else if( direction == FingerGestures.SwipeDirection.Right )
{
heading = Vector3.down;
//heading = Vector3.right;
}
else if( direction == FingerGestures.SwipeDirection.Left)
{
heading = Vector3.up;
//heading = Vector3.left;
}
this.gameObject.SendMessage("Rotation",heading*90);
print ("Swiped to Direction : "+direction);
}
void FingerGestures_OnPinchBegin( Vector2 fingerPos1, Vector2 fingerPos2 )
{
Pinching = true;
}
void FingerGestures_OnPinchMove( Vector2 fingerPos1, Vector2 fingerPos2, float delta )
{
if( Pinching)
{
// to zoom in/out camera on pinch
delta *=-(1f);
fov = Camera.main.fieldOfView;
fov += delta * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
Camera.main.fieldOfView = fov;
}
}
void FingerGestures_OnPinchEnd( Vector2 fingerPos1, Vector2 fingerPos2 )
{
if(Pinching)
{
Pinching= false;
}
}
void OnDisable () {
print ("Script was removed");
FingerGestures.OnFingerSwipe -= FingerGestures_OnFingerSwipe;
FingerGestures.OnPinchBegin -= FingerGestures_OnPinchBegin;
FingerGestures.OnPinchMove -= FingerGestures_OnPinchMove;
FingerGestures.OnPinchEnd -= FingerGestures_OnPinchEnd;
}
}
This code is to Rotate Cube in the swipe direction: (place this javascript in standard assets folder and attach this to the Cube) #pragma strict
enum MoveType {Time, Speed}
static var use : MoveObject;
//var startRotation;
var startRotation : Vector3;
var endRotation : Vector3;
var startTime : float;
var lerpInterval : float;
var lerpCalculator : float;
var isLerping : boolean;
function Awake () {
if (use) {
Debug.LogWarning("Only one instance of the MoveObject script in a scene is allowed");
return;
}
use = this;
}
function Translation (thisTransform : Transform, endPos : Vector3, value : float, moveType : MoveType) {
yield Translation (thisTransform, thisTransform.position, thisTransform.position + endPos, value, moveType);
}
function Translation (thisTransform : Transform, startPos : Vector3, endPos : Vector3, value : float, moveType : MoveType) {
var rate = (moveType == MoveType.Time)? 1.0/value : 1.0/Vector3.Distance(startPos, endPos) * value;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, t);
yield;
}
}
function Start()
{
// startRotation = this.transform.rotation;
}
function Rotation (degrees : Vector3) {
//print("Current Rot: "+ startRotation);
//var endRotation = this.transform.rotation * Quaternion.Euler(degrees);
startRotation = this.transform.localEulerAngles;
endRotation = (this.transform.localEulerAngles + degrees);
this.transform.localEulerAngles = endRotation;
lerpInterval = 1.5f;
startTime = Time.realtimeSinceStartup;
isLerping = true;
var rate = 1.0/1.5f;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * rate;
//this.transform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
this.transform.localEulerAngles = Vector3.Lerp(startRotation,endRotation,t);
yield;
}
print("X: " + this.transform.localEulerAngles.x);
if(this.transform.localEulerAngles.x <0.0f)
{
this.transform.localEulerAngles.x=0.0f;
print("X: " + this.transform.localEulerAngles.x);
}
print ("Last rotation of Cube: " + endRotation);
//startRotation = endRotation;
}