圆上的随机点

发布于 2024-12-29 16:27:35 字数 1178 浏览 0 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-01-05 16:27:35

您传递给 Random 构造函数的参数是随机种子,而不是数字范围。如果您想在每次启动游戏时生成新的随机数,请使用无参数构造函数。另外,仅声明一次随机数生成器。它使用时钟来初始化自身,但由于时钟的滴答速度非常慢(与 CPU 时钟频率相比),如果您每次创建一个新实例,它可能会多次生成相同的随机数。

static readonly Random random = new Random();

生成一个新角度

int angle = random.Next(360); // generates numbers in the range 0 ... 359

然后用或

double angle = 2.0 * Math.PI * random.NextDouble();

地雷坐标的公式为

mineX = centerX + radius * cos(angle)
mineY = centerY + radius * sin(angle)

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.

static readonly Random random = new Random();

Then generate a new angle with

int angle = random.Next(360); // generates numbers in the range 0 ... 359

or

double angle = 2.0 * Math.PI * random.NextDouble();

The formulas for the mine coordinates are

mineX = centerX + radius * cos(angle)
mineY = centerY + radius * sin(angle)
遮了一弯 2025-01-05 16:27:35

对于来这里寻找专门在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文