The issue is as follows:
I send the roll, pitch, and yaw of a real camera in world coordinates to Unity. In Unity, I receive these values and make a virtual object move in the same way.
This works well when the yaw value is 0
. However, when the yaw value rotates to approximately 90
degrees or -90
degrees, whether I rotate around the roll axis or the pitch axis, the object moves along the pitch axis.
update data
void Update()
{
if (positionUpdated)
{
if (Mathf.Abs(bRoll_ - Roll_) > 150 && flag == 2)
{
print("flag = 1 and bRoll_ - Roll_ : " + (bRoll_ - Roll_));
flag = 1;//바꾸기
}
else if (Mathf.Abs(bRoll_ - Roll_) > 150 && flag == 1)
{
print("flag = 2 and bRoll_ - Roll_ : " + (bRoll_ - Roll_));
flag = 2;//그대로
}
if (flag == 1)
{
Roll_2 = Roll_ + 180;
//bPitch_ 150
//Pitch_ 30
if (Pitch_ > 0)
{
Pitch_ = 180 - Pitch_;
}
else if (Pitch_ < 0)
{
Pitch_ = -180 - Pitch_;
}
//bYaw_: 150 bYaw : -30
//Yaw: 151 + 180 --> -29 Yaw : - 31 + 180 -> 149
//Yaw_ = Yaw_ - 180;
if (Yaw_ > 0)
{
Yaw_ = Yaw_ - 180;
}
else if (Yaw_ < 0)
{
Yaw_ = Yaw_ + 180;
}
print("flag = 1 : Rotate");
print("Roll_2 : " + Roll_2);
transform.position = targetPosition;
//transform.eulerAngles = targetRotation;
transform.eulerAngles = new Vector3(0, 0, 0);
print(" Pitch_ : " + Pitch_ + " Yaw_ : " + Yaw_ + " Roll_2 : " + -Roll_2);
transform.Rotate(Pitch_, 0.0f, 0.0f, Space.World);
transform.Rotate(0.0f, Yaw_, 0.0f, Space.World);
transform.Rotate(0.0f, 0.0f, -Roll_2, Space.World);
bRoll_ = Roll_2 - 180;
bPitch_ = Pitch_;
bYaw_ = Yaw_;
positionUpdated = false;
}
else
{
print("flag = 2 : Rotate");
transform.position = targetPosition;
//transform.eulerAngles = targetRotation;
transform.eulerAngles = new Vector3(0, 0, 0);
print(" Pitch_ : " + Pitch_ + " Yaw_ : " + Yaw_ + " Roll_ : " + -Roll_);
transform.Rotate(Pitch_, 0.0f, 0.0f, Space.World);
transform.Rotate(0.0f, Yaw_, 0.0f, Space.World);
transform.Rotate(0.0f, 0.0f, -Roll_, Space.World);
bRoll_ = Roll_;
bPitch_ = Pitch_;
bYaw_ = Yaw_;
positionUpdated = false;
}
}
else
{
transform.Rotate(0.0f, 0.0f, 0.0f, Space.World);
transform.Rotate(0.0f, 0.0f, 0.0f, Space.World);
transform.Rotate(0.0f, 0.0f, 0.0f, Space.World);
}
}
receive data
void ParseData(string data)
{
string[] values = data.Split(',');
Vector3 position = new Vector3(
-Convert.ToSingle(values[0]) + 0,
Convert.ToSingle(values[1]) + 0,
Convert.ToSingle(values[2]) + 0);
//Vector3 rotation = new Vector3(
// Convert.ToSingle(values[4]),
// -Convert.ToSingle(values[5]),
// -Convert.ToSingle(values[3]));
Roll_ = Convert.ToSingle(values[3]);
Pitch_ = Convert.ToSingle(values[4]);
Yaw_ = -Convert.ToSingle(values[5]);
targetPosition = position;
//targetRotation = rotation;
positionUpdated = true;
}
Rotating by Roll ⇒ The x-coordinate is the pitch, but the pitch has a Roll value:
Rotating by Pitch ⇒The x-coordinate is the pitch, so the pitch has a Pitch value:
What did I do wrong??? Is this a gimbal lock???
This is my issue page, and there are videos, and all codes.
thank you