有时候需要得知gameobject的位置与角度,可以透过GetComponent的函式来取得。
void Update () {
//Get the Gameobject Position data
PosX = GetComponent<Transform> ().position.x;
PosY = GetComponent<Transform> ().position.y;
PosZ = GetComponent<Transform> ().position.z;
//Get the Gameobject Angles data
RotX = GetComponent<Transform> ().eulerAngles.x;
RotY = GetComponent<Transform> ().eulerAngles.y;
RotZ = GetComponent<Transform> ().eulerAngles.z;
if (RotX > 180f) {
RotX = RotX - 360f;
}
if (RotY > 180f) {
RotY = RotY - 360f;
}
if (RotZ > 180f) {
RotZ = RotZ - 360f;
}
}
本文介绍了一种在Unity中通过GetComponent方法获取游戏对象位置和旋转角度的代码实现,包括将欧拉角转换为-180到180度范围内的方法。
1016

被折叠的 条评论
为什么被折叠?



