I'm currently working on a mobile game, it's a fairly simple core mechanic, you start with a single 3D cube and each turn you are given a new one that needs to be put or snapped to a face of a cube in the scene. You can select different faces of the cubes on the scene and the preview cube will spawn near that face so you can rotate it and find your desired placement. When you are happy with your choice just tap the "build cube button" the preview cube will disappear and the new one with the same values and rotation will appear on the selected face. I just need to rotate an object around a selected Axis 90 or -90 degrees. That its, but it is useless hehe, please if someone could help me it will be a lot, this is bothering me for days now. Thanks!
public class CubeBehaviour : MonoBehaviour
{
private Movement _movement;
private void Awake()
{
if (!GetComponent<Movement>())
Debug.LogWarning("Please add a movement script to the gameobject.");
_movement = GetComponent<Movement>();
}
public void Move(Vector3[] positions, Action callBack)
{
_movement.StartMove(positions,callBack);
}
[ContextMenu("Rotate X Positive")]
public void RotateAround_xAxisPositive()
{
Rotate(RotationAxis.X, true);
}
[ContextMenu("Rotate X Negative")]
public void RotateAround_xAxisNegative()
{
Rotate(RotationAxis.X, false);
}
[ContextMenu("Rotate Y Positive")]
public void RotateAround_yAxisPositive()
{
Rotate(RotationAxis.Y, true);
}
[ContextMenu("Rotate Y Negative")]
public void RotateAround_yAxisNegative()
{
Rotate(RotationAxis.Y, false);
}
[ContextMenu("Rotate Z Positive")]
public void RotateAround_zAxisPositive()
{
Rotate(RotationAxis.Z, true);
}
[ContextMenu("Rotate Z Negative")]
public void RotateAround_zAxisNegative()
{
Rotate(RotationAxis.Z, false);
}
public void Rotate(RotationAxis axis, bool positiveRotation)
{
StartCoroutine(DoRotation(axis,90, positiveRotation));
}
IEnumerator DoRotation(RotationAxis axis,float angles, bool positiveRotation)
{
float time = 1;
float elapsedTime = 0;
Quaternion destinationRotation = Quaternion.identity;
Vector3 anglesToRotate = Vector3.zero;
switch (axis)
{
case RotationAxis.X:
if (positiveRotation)
anglesToRotate = new Vector3(90, 0, 0);
else
anglesToRotate = new Vector3(-90, 0, 0);
break;
case RotationAxis.Y:
if (positiveRotation)
anglesToRotate = new Vector3(0, 90, 0);
else
anglesToRotate = new Vector3(0, -90, 0);
break;
case RotationAxis.Z:
if (positiveRotation)
anglesToRotate = new Vector3(0, 0, 90);
else
anglesToRotate = new Vector3(0, 0, -90);
break;
default:
Debug.LogError("You must assign a rotation axis!");
break;
}
destinationRotation *= this.transform.rotation * Quaternion.Euler(anglesToRotate);
Quaternion yRotation = Quaternion.identity;
Quaternion xRotation = Quaternion.identity;
Quaternion zRotation = Quaternion.identity;
while (elapsedTime < time)
{
yRotation = Quaternion.AngleAxis(anglesToRotate.y * Time.deltaTime, Vector3.up);
xRotation = Quaternion.AngleAxis(anglesToRotate.x * Time.deltaTime, Vector3.right);
zRotation = Quaternion.AngleAxis(anglesToRotate.z * Time.deltaTime, Vector3.forward);
this.transform.rotation = yRotation * xRotation * zRotation * this.transform.rotation;
elapsedTime += Time.deltaTime;
yield return null;
}
//this.transform.rotation = yRotation * xRotation * zRotation;
//this.transform.rotation = destinationRotation;
}
}
public enum RotationAxis
{
X,
Y,
Z
}
You are never supposed to set rotation like this, it will simply not work and the Unity docs warn about it as well.
this.transform.rotation = yRotation * xRotation * zRotation * this.transform.rotation;
If you are rotating one axis at a time you can use.
transform.Rotate(90.0f ,0.0f,0.0f,Space.World);
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
You can also use LookAt
transform.LookAt(targetCube);
https://docs.unity3d.com/ScriptReference/Transform.LookAt.html
You can also use Quaternion.Lerp
https://docs.unity3d.com/ScriptReference/Quaternion.Lerp.html
In general rotations in Unity can be tricky, especially if they are seemingly easy in the inspector.