我正在尝试在Unity中编写一个脚本,它在玩家直接面对的对象周围创建一种径向菜单,但菜单中的按钮数量是一个变量。
我已经生成了主菜单的角度,对象应该很容易出现......
// int buttonCount = number of buttons
float buttonWidth = 360 / buttonCount;
for (int i = 1; i <= buttonCount; i++)
{
float maxAngle = buttonWidth * i;
float minAngle;
if (i == 0)
{
minAngle = 0f;
}
else if (i == buttonCount)
{
minAngle = 360 - buttonWidth;
}
else
{
minAngle = buttonWidth * (i - 1);
}
float buttonAngle = (minAngle + maxAngle) / 2;
}
...但现在我正试图将按钮对象放在中央菜单对象周围的相应角度,我不知道怎么办?
答案 0 :(得分:3)
此功能将按钮所需的对象作为参数,播放器游戏对象,以便您可以将新按钮定向到播放器,按钮所需的角度以及半径(按钮的距离)将来自buttonCenter)。它的输出是世界空间中的按钮位置。您可以为要添加的每个按钮调用它。
Vector3 positionButton(GameObject buttonCenter, GameObject player, float angle, float radius) {
//get the up and right vectors from the player object so we can orient the buttons
Vector3 up = player.transform.up;
Vector3 right = player.transform.right;
angle = Mathf.Deg2Rad * angle; //convert degrees to radians. radians=degrees * 2pi / 360
//cos(angle) give an x coordinate, on a unit circle centered around 0
//sin(angle) is the y coordinate on the unit circle
//take those values, multiply them by the up and right vectors to orient them to the player,
//multiply by the radius to move them the correct distance from the buttoncenter,
//and add the buttoncenter position so they circle around the correct point
Vector3 buttonPos =buttonCenter.transform.position + (radius * right * Mathf.Cos(angle)) + (radius* up * Mathf.Sin(angle));
return buttonPos;
}
答案 1 :(得分:0)
首先,为每个按钮定义原点和距离。当你有角度时,你可以应用三角学,它可以让你找到给定角度,距离和原点的点的坐标。该点将由角度的cos()和sin()定义。