Okay everyone, I really need help. There is now maybe one month I’m on my problematic. So!
In a VR environement, I have a clone of the player guardian.
I have different waypoint and the player followw this this way.
My problem is the rotation of the scene. As we are in a VR environment, we have the boundary limitation. So, the different way point need to be in the guardian. Don’t worried about how we position this waypoint, it’s good.
The problem is the scene rotation. On the first point it’s okay, on the second point it’s not good at all. I have test many differents possibiities but… Always fail.
I really need your help.
Here my code:
/// <summary>
/// Calculate angle to apply rotation
/// </summary>
private void CalculateNewAngle(Vector3 nextDirection, Vector3 prevDirection)
{
if (nextDirection == Vector3.zero)
{
Debug.Log("nextDirection: " + nextDirection);
return;
}
if (prevDirection == Vector3.zero)
{
Debug.Log("PrevDirection: " + prevDirection);
return;
}
//Vector3 A_position = prevDirection;
Vector3 B_position = currentNode.NodeObject.transform.position;
//Vector3 C_position = nextDirection;
Vector3 BA = (prevDirection - B_position).normalized;
Vector3 BC = (nextDirection - B_position).normalized;
float dotProduct = Mathf.Clamp(Vector3.Dot(BA, BC), -1f, 1f);
float angleInRadian = Mathf.Acos(dotProduct);
float angleInDegrees = angleInRadian * Mathf.Rad2Deg;
angle = angleInDegrees;
Debug.Log(angle);
ApplyRotation(B_position);
}
/// <summary>
/// Apply the rotation of the scene
/// </summary>
private void ApplyRotation(Vector3 pivot)
{
//Vector3 pivotPosition = sceneAncor.position - currentNode.NodeObject.transform.position;
//Quaternion rotation = Quaternion.Euler(0, angle, 0);
//Vector3 rotatedPosition = rotation * pivotPosition;
//sceneAncor.position = currentNode.NodeObject.transform.position + rotatedPosition;
//sceneAncor.rotation = rotation * sceneAncor.rotation;
Vector3 pivotPosition = currentNode.NodeObject.transform.position;
sceneAncor.RotateAround(pivotPosition, Vector3.up, angle);
}
You can assume next and prev Direction are base on the next and previous waypoint to calculate the angle of rotation.