Hello to all!
The task is this:
There is a cube that we rotate along the 1st of 3 axes. The conditions of rotation are as follows: the axis along which we rotate the cube is considered free.
The angles on which the cube is turned should be a multiple of 90 degrees. But there is an exception, one face of the cube should be parallel to the player (or rather, the player's screen plane).
Now the problem: rotate the cube along the 1st of the three axes relative to the player:
myTransform - player rigidBody / rigidTransform - куб void FixedUpdate () { if (rigidBody){ Vector3 euler = rigidTransform.localEulerAngles;// rigidBody.rotation.eulerAngles;// rigidTransform.localRotation.eulerAngles;// Vector3 playerEulars = myTransform.eulerAngles;//rigidBody.rotation; Vector3 localAxis; Vector3 eulerAngleVelocity;// = localAxis * delta; if (axisIndex==0) { localAxis = rigidTransform.InverseTransformPoint (Vector3.up + rigidTransform.position); eulerAngleVelocity = localAxis * delta; euler.x = Mathf.Round (euler.x / 90f) * 90f; euler.z = Mathf.Round (euler.z / 90f) * 90f; Debug.Log ("OY"); } else { if (axisIndex==1) {// z axis localAxis = rigidTransform.InverseTransformPoint(myTransform.TransformPoint (Vector3.forward)+(rigidBody.position-myTransform.position)); eulerAngleVelocity = localAxis * delta; float displacement = euler.y - myTransform.eulerAngles.y; euler.y = playerEulars.y + Mathf.Round (displacement / 90f) * 90f; euler.x = Mathf.Round (euler.x / 90f) * 90f; Debug.Log ("OZ"); } else { if (axisIndex == 2) { localAxis = rigidTransform.InverseTransformPoint (myTransform.TransformPoint (Vector3.right) + (rigidBody.position - myTransform.position)); eulerAngleVelocity = localAxis * delta; float displacement = euler.y - myTransform.eulerAngles.y; euler.y = playerEulars.y + Mathf.Round (displacement / 90f) * 90f; euler.z = Mathf.Round (euler.z / 90f) * 90f; Debug.Log ("OX"); } else { eulerAngleVelocity = Vector3.zero; } } } rigidBody.rotation = Quaternion.Lerp (rigidBody.rotation, Quaternion.Euler(euler), 5f*Time.fixedDeltaTime); if (isLocalPlayer) { Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * 5000f * Time.fixedDeltaTime); rigidBody.rotation *= deltaRotation; } } }
1) The cube is constantly turned to one of the faces to the player 2) Rotates along one of the axes 3) Attempts to return to a multiple of 90 degrees along the other axes along which it is not rotated.
The problem is, the cube sometimes starts to juggle if you try to rotate it because rotations in the euler lead to this (he is incorrectly trying to return the angle) ... Do you understand what is happening? Advise how to fix it)