Hello all. I just faced a very strange behavior when calculating rotations using Quaternion class. FromToRotation gives me different results with the same input depending on where it was called.
Code of the method below:
public virtual bool TryHorizontalAimTo(Vector3 Target)
{
Target.y = Ship.transform.position.y;
Vector3 targetDirection = Vector3.Normalize(Target - Ship.transform.position);
Quaternion rotation = Quaternion.FromToRotation(_actualForwardDirection.normalized, targetDirection);
if (Ship.name == "PrototypeShip_2" && name == "TurretB")
{
Debug.Log("Target dir: " + targetDirection);
Debug.Log("Forward dir: " + _actualForwardDirection);
Debug.Log("Rotation: " + rotation.eulerAngles);
}
return TryApplyRotation(rotation);
}
If called from Start() and from FixedUpdate() this method produces the following logs:
If called from LateUpdate() it gives me the expected result of rotation around Y axis:
As you can see in both cases I have (0.7, 0, 0.7) and (-0.7, 0, -0.7) vectors but rotations are different for some reason. I have Rigidbody attached to the GameObject but it is IsKinematic in both cases so this should not be a problem.
How should I handle with this? Are any workarounds exist?
Thanks in advance.
UPDATE: I tested it more and it seems no matter when the method is called. It looks like the issue appears only when object rotated at 45 degrees or 225 degrees which leads to (0.7, 0, 0.7) and (-0.7, 0, -0.7) vector parameters passed to FromToRotation. With other angles it wors fine.