The intention of my code is to have the assigned rigid body facing whatever direction the player is facing while locked to the y-axis, using torque. So if the player looks left, the rigid body follows, etc.
public void DetermineRotation ()
{
Vector3 rbDirection = new Vector3(0f, transform.rotation.y, 0f);
Vector3 playerDirection = new Vector3(0f, player.transform.rotation.y, 0f);
//get the angle between board direction and player direction
float angleDiff = Vector3.Angle(rbDirection, playerDirection);
//cross product, the axis of rotation to get from one vector to another
Vector3 cross = Vector3.Cross(transform.forward, player.transform.right);
//apply torque along the cross product according to the magnitude of the angle
rb.AddTorque(cross * angleDiff * amount, ForceMode.Force);
}
At the moment, the script in this state allows my intent to function, but to a fault, as the rigid body is aligned to the player’s right. I have tried many different combinations to produce different cross products, but have unable to find one that aligns rb.transform.rotation.y with player.transform.rotation.y. Any and all help is appreciated!