I have worked with IMUs before and to my knowledge with Unity if the object is reading in the IMU data as quaternions, then you will not encounter the gimbo lock problem. So you can indeed create a reference to the object's rotation and convert those to euler angels, and then convert back to Quaternions before applying it to the object.
However, if you are simply wanting to limit rotation I would do something like this:
//the last rotation was pointed at the grey zone.
private Quaternion prevAllowedRotation;
void FixedUpdate(){
if(!isValidRotation()){
this.transform.rotation = prevAllowedRotation;
}else{
this.transform.lookAt(lockToArea());
}
}
private bool isValidRotation(){
//I chose forward based on the image, find the direction that works for you
Ray ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 10f){
if(hit.transform.tag == "wall"){
return true;
}
}
return false;
}
private Vector3 lockToArea(Gameobject grey){
Vector3 center = grey.transform.position;
//figure out how to get the position of the facing direction on the same plane as the grey area.
Vector3 pointerPosition = getPointerPosition();
RaycastHit hit;
if(Physics.Linecast(pointerPosition, center, hit)){
return hit.point;
}
return Vector3.zero;
}
private Vector3 getPointerOnPlane(){
Ray ray = new Ray(this.transform.position, this.transform.forward);
// you need to figure out how to dyanmically get the distance so that the ray lands on the same plane as the vector
float distance = 0;
return ray.GetPoint(distance);
}
This locks its rotation to only be pointing at the grey cube. This should help you a lot, you just need to get the point that the IMU data is reading in at and is on the same plane as the grey cube's center.
Quaternion rotation = Quaternion.Euler(new Vector3(transform.eulerAngles.x, 0, 0));be applicable?wxyzvalues won't help at all as individually they're incomplete. Best I can think of is to calculate a EulerAngleTo()(you'll have to write this method) on the two blue boxes and check that both the X and Y components thereof are greater than 0 (or lessthan, as appropriate).