If rotation was over 20° smoothly rotate towards the next 90°

Hey Unity Pros,

So basically what i want to achieve is I want my 3d cube to rotate towards its next 90° (0,90,180,270,360) if the touch rotation was greater than 20°. Now I’ve googled everything but nothing popped up that could solve my problem.however I did found out how to do this with 45°(with mathf.round) but that was way to much for me.

I’d really appreciate it if someone could help me with this because I’ve been struggling with this for quite some time now…

Are you saying you want to round up the rotation to the nearest multiple of 90 if it’s greater than 20? If that’s the case, you can do something like:

``````if(Number>20)
{
Number = (int) Math.Ceiling(Number / 90) * 90;
}
``````

If you want to perform the rotation smoothly, instead of snapping, you can look up interpolation, such as Mathf.Lerp. Alternatively, you can use third party tweening plugins such as DOTween which I highly recommend.

hey brain56,

(i had to post this as an answer because it was way too long for a comment thanks to the script -_-)

thanks for your answer! i have implemented your solution into my code and it works but also it doesnt at the same time. now if all the angles are 0 than rotation on the y and z axis are great but not on th x. also if you rotate eg: the y axis to 90 than the rotation gets all weird and rather than rotating on the axis i want it rotates to the direction i dont want to… anyways… here is my code so you can take a look at it and tell me where have i fxcked it up. oh and im working on mobile so the script its touch based.

``````using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using DG.Tweening;

public class rotate : MonoBehaviour {

public Image debug_go;
public Image hehi;
public Image roth;
public Image rotvl;
public Image rotvr;
public Text zeroed;
public Text horSwipe;
public Text horSwipeL;

public float rotSpeed;

public bool isRotatingHorizontal = false;
public bool isRotatingVerticalL = false;
public bool isRotatingVerticalR = false;

public bool startPosDone = false;

public float swipeDistHorizontal;
public float swipeDistVertical;

public GameObject targetItem;

public float minSwipeDistX;
public float minSwipeDistY;
private Vector2 startPos;
public Vector3 endPos;

public float lerpspeed;

float dist;

public Vector3 startRot;
bool once;
bool touched;

float angle;
Vector2 startPos2nd;
public bool heh = false;

Vector3 currentangle;

void Update () {

if (targetItem.GetComponent<Rigidbody> ().IsSleeping ())
debug_go.enabled = false;
else
debug_go.enabled = true;
if (heh)
hehi.enabled = true;
else
hehi.enabled = false;
if (isRotatingHorizontal)
roth.enabled = true;
else
roth.enabled = false;
if (isRotatingVerticalL)
rotvl.enabled = true;
else
rotvl.enabled = false;
if (isRotatingVerticalR)
rotvr.enabled = true;
else
rotvr.enabled = false;

zeroed.text = targetItem.transform.eulerAngles.ToString ();
horSwipeL.text = swipeDistHorizontal.ToString () + "x, " + swipeDistVertical.ToString () + "y";
horSwipe.text = angle.ToString ();

if (heh) {

Vector3 vec = targetItem.transform.localEulerAngles;

if (isRotatingHorizontal && !isRotatingVerticalL && !isRotatingVerticalR) {

if (endPos.x > 0) {

if (angle >= 100) {
vec.y = Mathf.Floor (vec.y / 90) * 90;
}
else if (angle < 100) {
vec.y = Mathf.Ceil (vec.y / 90) * 90;
}
}

else if (endPos.x < 0f) {

if (angle >= 100) {
vec.y = Mathf.Floor (vec.y / 90) * 90;
}
else if (angle < 100) {
vec.y = Mathf.Ceil (vec.y / 90) * 90;
}
}

vec.x = Mathf.Round(vec.x / 90) * 90;
vec.z = Mathf.Round(vec.z / 90) * 90;
}

else if (isRotatingVerticalL && !isRotatingVerticalR && !isRotatingHorizontal) {

if (endPos.y < 0f) {

if (angle >= 100) {
vec.x = Mathf.Floor (vec.x / 90) * 90;
}
else if (angle < 100) {
vec.x = Mathf.Ceil (vec.x / 90) * 90;
}
}

else if (endPos.y > 0f) {

if (angle >= 100) {
vec.x = Mathf.Floor (vec.x / 90) * 90;
}
else if (angle < 100) {
vec.x = Mathf.Ceil (vec.x / 90) * 90;
}
}

vec.z = Mathf.Round(vec.z / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
}

else if (isRotatingVerticalR && !isRotatingVerticalL && !isRotatingHorizontal) {

if (endPos.y < 0f) {

if (angle >= 100) {
vec.z = Mathf.Floor (vec.z / 90) * 90;
}
else if (angle < 100) {
vec.z = Mathf.Ceil (vec.z / 90) * 90;
}
}

else if (endPos.y > 0f) {

if (angle >= 100) {
vec.z = Mathf.Floor (vec.z / 90) * 90;
}
else if (angle < 100) {
vec.z = Mathf.Ceil (vec.z / 90) * 90;
}
}
vec.x = Mathf.Round(vec.x / 90) * 90;
vec.y = Mathf.Round(vec.y / 90) * 90;
}

//targetItem.transform.localEulerAngles = Vector3. (targetItem.transform.localEulerAngles, vec, rotSpeed * Time.deltaTime);
targetItem.transform.DORotate (vec, rotSpeed * Time.deltaTime, RotateMode.Fast);

if (isRotatingHorizontal) {

if (targetItem.transform.localEulerAngles.y < 0.01f && targetItem.transform.localEulerAngles.y > -0.01f ||
targetItem.transform.localEulerAngles.y < 90.01f && targetItem.transform.localEulerAngles.y > 89.99f ||
targetItem.transform.localEulerAngles.y < 180.01f && targetItem.transform.localEulerAngles.y > 179.99f ||
targetItem.transform.localEulerAngles.y < 270.01f && targetItem.transform.localEulerAngles.y > 269.99f ||
targetItem.transform.localEulerAngles.y < 360.01f && targetItem.transform.localEulerAngles.y > 359.99f ||
targetItem.transform.localEulerAngles.y < 0.01f && targetItem.transform.localEulerAngles.y > -0.01f ||
targetItem.transform.localEulerAngles.y > -90.01f && targetItem.transform.localEulerAngles.y < -89.99f ||
targetItem.transform.localEulerAngles.y > -180.01f && targetItem.transform.localEulerAngles.y < -179.99f ||
targetItem.transform.localEulerAngles.y > -270.01f && targetItem.transform.localEulerAngles.y < -269.99f ||
targetItem.transform.localEulerAngles.y > -360.01f && targetItem.transform.localEulerAngles.y < -359.99f) { //this is to correct floating points and snap

heh = false;
isRotatingHorizontal = false;
isRotatingVerticalR = false;
isRotatingVerticalL = false;
startPosDone = false;
once = false;
}
}
else if (isRotatingVerticalL) {

if (targetItem.transform.localEulerAngles.x < 0.01f && targetItem.transform.localEulerAngles.x > -0.01f ||
targetItem.transform.localEulerAngles.x < 90.01f && targetItem.transform.localEulerAngles.x > 89.99f ||
targetItem.transform.localEulerAngles.x < 180.01f && targetItem.transform.localEulerAngles.x > 179.99f ||
targetItem.transform.localEulerAngles.x < 270.01f && targetItem.transform.localEulerAngles.x > 269.99f ||
targetItem.transform.localEulerAngles.x < 360.01f && targetItem.transform.localEulerAngles.x > 359.99f ||
targetItem.transform.localEulerAngles.x < 0.01f && targetItem.transform.localEulerAngles.x > -0.01f ||
targetItem.transform.localEulerAngles.x > -90.01f && targetItem.transform.localEulerAngles.x < -89.99f ||
targetItem.transform.localEulerAngles.x > -180.01f && targetItem.transform.localEulerAngles.x < -179.99f ||
targetItem.transform.localEulerAngles.x > -270.01f && targetItem.transform.localEulerAngles.x < -269.99f ||
targetItem.transform.localEulerAngles.x > -360.01f && targetItem.transform.localEulerAngles.x < -359.99f) { //this is to correct floating points and snap

heh = false;
isRotatingHorizontal = false;
isRotatingVerticalR = false;
isRotatingVerticalL = false;
startPosDone = false;
once = false;
}
}
else if (isRotatingVerticalR) {

if (targetItem.transform.localEulerAngles.z < 0.01f && targetItem.transform.localEulerAngles.z > -0.01f ||
targetItem.transform.localEulerAngles.z < 90.01f && targetItem.transform.localEulerAngles.z > 89.99f ||
targetItem.transform.localEulerAngles.z < 180.01f && targetItem.transform.localEulerAngles.z > 179.99f ||
targetItem.transform.localEulerAngles.z < 270.01f && targetItem.transform.localEulerAngles.z > 269.99f ||
targetItem.transform.localEulerAngles.z < 360.01f && targetItem.transform.localEulerAngles.z > 359.99f ||
targetItem.transform.localEulerAngles.z < 0.01f && targetItem.transform.localEulerAngles.z > -0.01f ||
targetItem.transform.localEulerAngles.z > -90.01f && targetItem.transform.localEulerAngles.z < -89.99f ||
targetItem.transform.localEulerAngles.z > -180.01f && targetItem.transform.localEulerAngles.z < -179.99f ||
targetItem.transform.localEulerAngles.z > -270.01f && targetItem.transform.localEulerAngles.z < -269.99f ||
targetItem.transform.localEulerAngles.z > -360.01f && targetItem.transform.localEulerAngles.z < -359.99f) { //this is to correct floating points and snap

heh = false;
isRotatingHorizontal = false;
isRotatingVerticalR = false;
isRotatingVerticalL = false;
startPosDone = false;
once = false;
}
}
}

if (Input.touchCount > 0) {

Touch touch = Input.touches [0];

switch (touch.phase) {

case TouchPhase.Began:

startPos = touch.position;
startPos2nd = touch.position;

angle = 0f;

if (heh == false) {
isRotatingHorizontal = false;
isRotatingVerticalR = false;
isRotatingVerticalL = false;
startPosDone = false;
}
break;

case TouchPhase.Moved:

if (!startPosDone && !heh) {

swipeDistHorizontal = Vector3.Distance (new Vector3 (startPos.x, 0, 0), new Vector3 (touch.position.x, 0, 0));
swipeDistVertical = Vector3.Distance (new Vector3 (0, startPos.y, 0), new Vector3 (0, touch.position.y, 0));
}

if (swipeDistHorizontal > minSwipeDistX && !isRotatingVerticalR && !isRotatingVerticalL) {

if (startPosDone && !once) {
startPos2nd.x -= swipeDistHorizontal;
once = true;
}
targetItem.transform.Rotate (0, -(touch.deltaPosition.x / 1.5f), 0, Space.Self);
angle = Vector3.Distance (new Vector3 (startPos2nd.x, 0, 0), new Vector3 (touch.position.x, 0, 0));
isRotatingHorizontal = true;
startPosDone = true;
}

else if (swipeDistVertical > minSwipeDistY && isRotatingHorizontal == false) {

if (startPos.x < Screen.width / 2 && !isRotatingVerticalR) {

if (startPosDone && !once) {
startPos2nd.y -= swipeDistVertical;
once = true;
}

targetItem.transform.Rotate ((touch.deltaPosition.y / 1.5f), 0, 0, Space.Self);
angle = Vector3.Distance (new Vector3 (0, startPos2nd.y, 0), new Vector3 (0, touch.position.y, 0));
isRotatingHorizontal = false;
isRotatingVerticalL = true;
isRotatingVerticalR = false;
startPosDone = true;
}

else if (startPos.x > Screen.width / 2 && !isRotatingVerticalL) {

if (startPosDone && !once) {
startPos2nd.y -= swipeDistVertical;
once = true;
}

targetItem.transform.Rotate (0, 0, (touch.deltaPosition.y / 1.5f), Space.Self);
angle = Vector3.Distance (new Vector3 (0, startPos2nd.y, 0), new Vector3 (0, touch.position.y, 0));
isRotatingHorizontal = false;
isRotatingVerticalL = false;
isRotatingVerticalR = true;
startPosDone = true;
}
}
break;

case TouchPhase.Ended:

if (swipeDistHorizontal > minSwipeDistX || swipeDistVertical > minSwipeDistY) {
heh = true;
endPos = startPos - touch.position;
}
break;
}
}
}
}
``````