- Home /
Can't figure out local rotation axis.
Hello to all,
Here's my setup: I have a cube which has 6 exterior faces which can be rotated individually along their Y (up) axis. Here are the cubes showcasing their pivot and local axis
The faces are rotated on Y axis with mouse drag with the following code:
Vector3 _mouseOffset = (Input.mousePosition - _mousePos);
Vector3 _rotation = Vector3.zero;
_rotation.y = (_mouseOffset.x + _mouseOffset.y) * rotationSpeedEditor;
_selectedFace.transform.Rotate(_rotation, Space.Self);
_mousePos = Input.mousePosition;
Now here's the problem. Upon releasing the mouse, I want to smoothly rotate the face to the closest value between 0, 90, 180, 270 but assigning the localRotation I don't get the desired effect because one face is rotating on the local Y axis and the other on the local X axis. Shouldn't both of them be using the local Y for rotation?
Here's the code I use upon releasing the mouse button
Vector3 currentRotation =_selectedFace.transform.localRotation.eulerAngles;
float currentRotationY = currentRotation.y;
if(currentRotationY <= 45)
{
currentRotation.y = 0;
}
else if (currentRotationY <= 135)
{
currentRotation.y = 90;
}else if (currentRotationY <= 225)
{
currentRotation.y = 180;
}else if (currentRotationY <= 315)
{
currentRotation.y = 270;
}
else
{
currentRotation.y = 0;
}
_selectedFace.transform.DOLocalRotate(currentRotation, .2f);
I know I am missing something here but cannot figure out what. Any help is highly appreciated.
EDIT: Just for clarification, I am wondering why rotating the face on the local Y axis, Unity reports it as rotating on the X axis. Added image to showcase the issue: Local Rotation With Log
Answer by JonPQ · Apr 20, 2020 at 11:35 PM
try using Transform.RotateAround(). you can specify which axis to rotate around... specify the world Y axis (the "Normal" vector) for each respective surface. https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html
Still not sure what your problem is.... is it (A) the face rotates on incorrect axis and swings open like a door? or (b) it still visually rotates on the correct axis, but not to where you drag it. or (c) it still visually rotates on the correct axis, but unity reports in the inspector window a different axis rotated than expected that rotated ? Do you have a screenshot of broken result ?
Your answer
Follow this Question
Related Questions
Copy local rotation on one axis of A to B 1 Answer
How to clamp character rotation on Y and X axis? 1 Answer
Combining 2 Rotations: Global and Local 2 Answers
Instantiating an object in front of the player 3 Answers
Unity Simulate Local Rotation 0 Answers