圆上的随机点
我正在尝试在 Unity 中开发一款游戏,您可以从 2D 星球跳到 2D 星球,每个星球都有自己的引力(从技术上讲,游戏是 2.5D,但所有运动都是沿着 X 和 Y 轴)。我想使用参数公式在这些行星上的随机点放置地雷;这是我开发的脚本,用于将它们附加到父 Planet 对象。然而,地雷并没有像预期那样出现在圆圈表面,而是形状非常扭曲。我可能做错了什么?
public class LandMine : MonoBehaviour
{
public GameObject mine;
private GameObject landmine;
private System.Random rand;
private Vector3 pos;
List<GameObject> mines;
public void Start()
{
mines = new List<GameObject>();
LevelStart();
}
public Vector3 ran()
{
rand = new System.Random(359);
float angle = rand.Next();
float value = angle * (Mathf.PI/180f);
float x = (float) (0.5000001 * Mathf.Cos(value)) + 6;
float y = (float) (0.5000001 * Mathf.Sin(value)) - 9;
return new Vector3(x,y,0);
}
void LevelStart()
{
for (int i = 0; i < 5; i++)
{
pos = ran;
mine = Instantiate(mine, pos,Quaternion.identity) as GameObject;
mines.Add(mines);
}
foreach (GameObject m in mines)
{
m.transform.parent = this.transform;
}
}
}
I am trying to develop a game in Unity where you jump from 2D planet to 2D planet, each with its own gravitational pull (The game is 2.5D, technically, but all movement is along the X and Y axes). I would like to place landmines at random points along these planets using the parametric formula; this is the script that I've developed to attach them to the parent Planet object. However, mines are not appearing at the surface of the circles as expected, and are instead appearing very distorted in shape. What might I be doing wrong?
public class LandMine : MonoBehaviour
{
public GameObject mine;
private GameObject landmine;
private System.Random rand;
private Vector3 pos;
List<GameObject> mines;
public void Start()
{
mines = new List<GameObject>();
LevelStart();
}
public Vector3 ran()
{
rand = new System.Random(359);
float angle = rand.Next();
float value = angle * (Mathf.PI/180f);
float x = (float) (0.5000001 * Mathf.Cos(value)) + 6;
float y = (float) (0.5000001 * Mathf.Sin(value)) - 9;
return new Vector3(x,y,0);
}
void LevelStart()
{
for (int i = 0; i < 5; i++)
{
pos = ran;
mine = Instantiate(mine, pos,Quaternion.identity) as GameObject;
mines.Add(mines);
}
foreach (GameObject m in mines)
{
m.transform.parent = this.transform;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您传递给 Random 构造函数的参数是随机种子,而不是数字范围。如果您想在每次启动游戏时生成新的随机数,请使用无参数构造函数。另外,仅声明一次随机数生成器。它使用时钟来初始化自身,但由于时钟的滴答速度非常慢(与 CPU 时钟频率相比),如果您每次创建一个新实例,它可能会多次生成相同的随机数。
生成一个新角度
然后用或
地雷坐标的公式为
The parameter you pass to the Random constructor is the random seed, not the range of numbers. If you want to generate new random numbers each time you start the game, use the parameter-less constructor. Also, declare the random number generator only once. It uses the time clock in order to initialize itself, but since the clock ticks very slowly (compared to the CPU clock frequency) it could generate the same random number several times if you create a new instance each time.
Then generate a new angle with
or
The formulas for the mine coordinates are
对于来这里寻找专门在 Unity 中获取圆内随机点的人来说,请注意:
我不知道首次发布此内容时它是否存在,但您可以使用
UnityEngine.Random.insideUnitCircle()
UnityEngine.Random.insideUnitCircle() code> 获取半径为 1 的圆内的随机点。还有一个 insideUnitSphere 用于获取 3D 球体中的随机点。希望有帮助。
Just a note for anyone coming here looking to get a random point inside a circle specifically in Unity:
I don't know if it existed when this was first posted, but you can use
UnityEngine.Random.insideUnitCircle()
to get a random point inside a circle with a radius of 1. There's also an insideUnitSphere for getting a random point in a 3D sphere.Hope that helps.