I want a rigidbody to add force relative to camera forward on XZ axis from global forward axis.
But the problem is it doesn’t result in right rotation of directionTo.
Code:
if (Input.GetButton(button))
{
Vector3 directionTo = direction;
if (relativeToCamera)
{
Vector3 cameraForward = new Vector3(camera.forward.x, 0, camera.forward.z);
Quaternion rotation = Quaternion.LookRotation(Vector3.forward, cameraForward);
directionTo = rotation * direction;
}
Debug.Log(directionTo.ToString()); // (1.0, 0.0, 0.0) // first frame
Debug.Log(directionTo.ToString()); // (0.0, -1.0, 0.0) // other frames
rigidbody.AddForce(directionTo.normalized * force);
}