So I’m making a table that has four billoard legs. Each one of this legs should be detachable and have it’s own box collider. That’s why I make the billboard planes child of the “physical” legs.
Now I need to make the LegPlanes billboard objects look at the camera, but rotate only on one axis.
I did something like this:
void RotateToCamera()
{
//rotating leg billboard with leg parent
Plane.transform.rotation=transform.rotation;
//taking it's local rotation in this state
float xRot=Plane.transform.localEulerAngles.x;
float yRot=Plane.transform.localEulerAngles.y;
float zRot=Plane.transform.localEulerAngles.z;
//now rotating billboard with the camera
Plane.transform.forward=Camera.main.transform.forward;
//setting new local rotation on all axises. if object can rotate in certain axis
//(AxisCanRot variable) it's getting local value from leg parent, if can't rotate
// on certain axis it takes local rotation affected by camera position
float quatX; float quatY; float quatZ;
if(xAxisCanRot){quatX=xRot;}else{quatX=Plane.transform.localEulerAngles.x}
if(yAxisCanRot){quatY=yRot;}else{quatY=Plane.transform.localEulerAngles.y}
if(zAxisCanRot){quatZ=zRot;}else{quatZ=Plane.transform.localEulerAngles.z}
//now I put them all together as a new local rotation
Plane.transform.localRotation=Quaternion.Euler(quatX,quatY,quatZ);
}
In this code im having xAxisCanRot and yAxisCanRot true, and zAxisCanRot false, so legs will only rotate around local z axis.
It all seems to work, Im rotating camera around chair and it works as intended, but when I knock over the chair and legs are upside-down or sideways there are some weird bugs.
The legs rool no longer follows the camera, but kinda other way round.
Is there a way to do this right? Maybe it’s because legs are child to another parent, but I detached them and still they are acting the same