Strange behavior on cube rotations
I'm making a simple puzzle that requires cube game objects to rotate on X and Z (Not on Y, as the cubes lay flat on a plane so to speak, you look down on them as the user from a bird's eye view down the Y axis). In the code below, I'm doing the following:
On begin drag of cube: (left, right, up or down on screen), I grab the starting transform angles X, Y and Z.
On drag: I figure out which direction the user is dragging, then start to rotate the cube in that direction as the user drags.
On drag end (finger lift or mouse click ends): I snap the cube to the new position based on the direction it was being dragged and where it started.
So, sounds simple enough, right? The problem I'm having is that dragging left or right works great from the start, as long as I only drag left or right (rotates around Z axis). The cube snaps into the correct position (I have a cube with 6 sides mapped on differently so I can actually see the 6 different sides with color and words on them). When I drag down (rotate around X axis), it works only once, then the second time it is as if the axis has changed on the snap to portion, so it gets messed up. It still drags correctly, but on drag end, it snaps as if it is rotating around the wrong axis.
I think this is because when I rotate down or up around X axis, I'm changing the game object relative Y and Z axis orientation..... but how do i stop it from doing this?!?
Not sure if I explained this well, but I hope code below of what I'm talking about will help:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class cubeMove : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
{
bool draggingRight = false;
bool draggingLeft = false;
bool draggingUp = false;
bool draggingDown = false;
public float dragFactor = 30f;
float startX;
float startY;
float startZ;
private enum DraggedDirection
{
Up,
Down,
Right,
Left
}
public void OnBeginDrag(PointerEventData eventData)
{
startX = transform.eulerAngles.x;
startY = transform.eulerAngles.y;
startZ = transform.eulerAngles.z;
Debug.Log(startX + " and " + startY + " and " + startZ);
}
public void OnDrag(PointerEventData eventData)
{
Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
var currentDir = GetDragDirection(dragVectorDirection);
string currentDirS = currentDir.ToString();
if (currentDirS == "Left")
{
transform.Rotate(0, 0, Time.deltaTime * dragFactor, Space.World);
if (draggingLeft == false)
{
draggingLeft = true;
draggingRight = false;
draggingUp = false;
draggingDown = false;
}
}
if (currentDirS == "Right")
{
transform.Rotate(0, 0, Time.deltaTime * -dragFactor, Space.World);
if (draggingRight == false)
{
draggingLeft = false;
draggingRight = true;
draggingUp = false;
draggingDown = false;
}
}
if (currentDirS == "Up")
{
transform.Rotate(Time.deltaTime * dragFactor, 0, 0, Space.World);
if (draggingUp == false)
{
draggingLeft = false;
draggingRight = false;
draggingUp = true;
draggingDown = false;
}
}
if (currentDirS == "Down")
{
transform.Rotate(Time.deltaTime * -dragFactor, 0, 0, Space.World);
if (draggingDown == false)
{
draggingLeft = false;
draggingRight = false;
draggingUp = false;
draggingDown = true;
}
}
}
public void OnEndDrag(PointerEventData eventData)
{
if (draggingLeft == true)
{
transform.eulerAngles = new Vector3 (startX, startY, startZ + 90f);
draggingRight = false;
draggingLeft = false;
draggingUp = false;
draggingDown = false;
}
if (draggingRight == true)
{
transform.eulerAngles = new Vector3(startX, startY, startZ - 90f);
draggingRight = false;
draggingLeft = false;
draggingUp = false;
draggingDown = false;
}
if (draggingUp == true)
{
transform.eulerAngles = new Vector3(startX + 90f, startY, startZ);
draggingRight = false;
draggingLeft = false;
draggingUp = false;
draggingDown = false;
}
if (draggingDown == true)
{
transform.eulerAngles = new Vector3(startX - 90f, startY, startZ);
draggingRight = false;
draggingLeft = false;
draggingUp = false;
draggingDown = false;
}
}
private DraggedDirection GetDragDirection(Vector3 dragVector)
{
float positiveX = Mathf.Abs(dragVector.x);
float positiveY = Mathf.Abs(dragVector.y);
DraggedDirection draggedDir;
if (positiveX > positiveY)
{
draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left;
}
else
{
draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down;
}
//Debug.Log(draggedDir);
return draggedDir;
}
}