Skip to content

Instantly share code, notes, and snippets.

@yubeneko
Created October 30, 2019 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yubeneko/e8a83c6a024fc140aecda3178c8c5878 to your computer and use it in GitHub Desktop.
Save yubeneko/e8a83c6a024fc140aecda3178c8c5878 to your computer and use it in GitHub Desktop.
Unityの角度 -> サーボの角度の変換コード。
using UnityEngine;
public class HeadRotation : MonoBehaviour
{
public string GetServoAngle ()
{
return $"{ConvertPitch(transform.localEulerAngles.x)},{ConvertYaw(transform.localEulerAngles.y)}";
}
int ConvertPitch (float unityAngleX)
{
var servoPitch = 0f;
if (270f <= unityAngleX && unityAngleX < 360f)
{
servoPitch = unityAngleX - 270f;
}
else if (0f <= unityAngleX && unityAngleX <= 90f)
{
servoPitch = unityAngleX + 90f;
}
return (int)servoPitch;
}
int ConvertYaw (float unityAngleY)
{
var servoYaw = 0f;
if (0f <= unityAngleY && unityAngleY < 90f)
{
servoYaw = 90f - unityAngleY;
}
else if (90f <= unityAngleY && unityAngleY < 180f)
{
servoYaw = 0f;
}
else if (180f <= unityAngleY && unityAngleY < 270f)
{
servoYaw = 180f;
}
else
{
servoYaw = 270f - (unityAngleY - 180f);
}
return (int)servoYaw;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment