I’ve got an IMU controlling the rotation of the cube (on the right) via a Quaternion, works perfect…
I would like to copy only the localRotation around the red axis (for example the Transform.forward axis) of the cube, to the red axis on the plane (on the left).
If for example the rotation around the blue axis of the cube changes (via the IMU), the rotation of the plane shouldn’t change.
I can’t get my head around this one!
You could do it by taking Hadamard Product of desired axis of rotation for the plane and Euler angle component of the cube.Doing that will eliminate the values in all other axes than the desired one .And then update the plane’s rotation with the newly obtained vector.
v1 = (x, y, z)
v2 =transform.forward =(0, 0, 1)
Hadamard product of v1 and v2 gives (0, 0, z)
I have done the following implementation.Take a look and I hope it helps.
public class HandleRotation : MonoBehaviour
{
public Transform _CubeTransform;
public Transform _QuadTransform;
void Update () {
//Controllig the Cube's rotation with keyboard
if(Input.GetKey(KeyCode.A))
_CubeTransform.Rotate(_CubeTransform.forward,Space.Self);
if(Input.GetKey(KeyCode.D))
_CubeTransform.Rotate(_CubeTransform.forward*-1,Space.Self);
if(Input.GetKey(KeyCode.W))
_CubeTransform.Rotate(_CubeTransform.up,Space.Self);
if(Input.GetKey(KeyCode.S))
_CubeTransform.Rotate(_CubeTransform.up*-1,Space.Self);
//Update quad's rotation in the desired axis (Vector3.forward here )
UpdateRotation(_QuadTransform,_QuadTransform.forward);
print(transform.forward);
}
private void UpdateRotation(Transform target,Vector3 axis)
{
Vector3 rot = _CubeTransform.rotation.eulerAngles;
rot = Hadamard(rot,axis);
target.rotation = Quaternion.Euler(rot);
}
//returns hadamard product of two vectors
private Vector3 Hadamard(Vector3 v1, Vector3 v2)
{
return new Vector3(v1.x*v2.x,v1.y*v2.y,v1.z*v2.z);
}
}