Smooth cube rotation exactly 90 degrees
Hello, I am new to Unity3D and want to improve myself. I try to rotate a cube smoothly on 90 degrees around its own axis if button is clicked. It is easy on the first look, but rotating the cube its local axis are changed in the global space, so when I try to rotate it around Y axis (Vector2.up) I expect it will be rotated in all cases around the global Y axis that is not changed, because the local Y is on another place in the space.
You can see better on the picture what I mean. Here the cube Y axis is more like X axis, but again I want to rotate around default Y axis that in this case is Z axis. After many rotations I do not know where is my Y, X or Z axes. Can you help me ?
Thanks
Answer by doublemax · Nov 09, 2016 at 01:13 PM
transform.RotateAround(transform.position, Vector3.up, 20 * Time.deltaTime);
Yes, but I want also clicking on the cube smooth rotation on exactly 90 degrees.
using System.Collections;
using UnityEngine;
public class RotateTest : $$anonymous$$onoBehaviour
{
void Update ()
{
if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) )
{
StartCoroutine( RotateAround( Vector3.up, 90.0f, 1.0f) );
}
}
IEnumerator RotateAround( Vector3 axis, float angle, float duration )
{
float elapsed = 0.0f;
float rotated = 0.0f;
while( elapsed < duration )
{
float step = angle / duration * Time.deltaTime;
transform.RotateAround(transform.position, axis, step );
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
transform.RotateAround(transform.position, axis, angle - rotated );
}
}
You should also check the LeanTween or iTween assets which are both free and turn tasks like this into a one-liner.
Thanks doublemax! It works, but rotating the cube it changes also its angle in the space. I need when the cube is rotated in the space and XYZ axes are in totally different place from its original, again to rotate on the vertical axis, that can be Z or X or Y. Also i will check the ITween and LeanTween, thank you about this information
Answer by evrov · Nov 09, 2016 at 02:58 PM
On the attached picture my Y axis is more like x axis in the global space. So I want to rotate vertical again and in this case around Z axis, because it is the vertical in the current situation.
Your answer
Follow this Question
Related Questions
How do I limit rotations for both positive and negative degrees? 1 Answer
How do I smooth motion over a cube connection? 0 Answers
How to smoothly change position 1 Answer
Why isn't my object lerping ? C# 2 Answers
models problem always "stepped" 0 Answers