简体   繁体   English

关于实例化的建议?

[英]Advice on Instantiation?

This is my first question on Stack Overflow.这是我关于 Stack Overflow 的第一个问题。 I am quite the coding newbie, so please bear with me and my horrific code.我是编码新手,所以请忍受我和我可怕的代码。

void ChickenInstantiate()
    {
        LocOfChkn.Add(spawnLoc);
        int i = 0;
        for (int ChickenCount = 0; ChickenCount < maxChickenCount; ChickenCount++)
        {
            while (Vector3.Distance(spawnLoc, LocOfChkn[i]) < 2)
            {
                spawnLoc = new Vector3(Random.Range(randXMin, randXMax), Random.Range(randYMin, randYMax), 1);
                spawnLoc.z = 5;
                i += 1;
                if (i >= LocOfChkn.Count)
                {
                    break;
                }
            }
            Instantiate(ChickenPrefab1, spawnLoc, Quaternion.identity);
            LocOfChkn.Add(spawnLoc);
            i = 0;
        }
    }

This is some code for instating a prefab but making sure it doesn't instantiate within an area, and frankly I'm not sure how I should fix it- its completely broken;这是一些用于安装预制件的代码,但要确保它不会在一个区域内实例化,坦率地说,我不确定我应该如何修复它——它完全坏了; chickens don't appear, I'm not even sure if they spawn in the correct areas, etc. (the first instance of spawnLoc is defined at startup, btw)鸡没有出现,我什至不确定它们是否在正确的区域产卵,等等(spawnLoc 的第一个实例是在启动时定义的,顺便说一句)

I'm using the current latest version of unity and visual studio.我正在使用当前最新版本的统一和视觉工作室。

  • You should Instantiate a chicken for the first location you add to locOfChkn .您应该为添加到locOfChkn的第一个位置Instantiate一只鸡。 This will serve as both making sure your code is running, and avoid a chickenless spot.这既可以确保您的代码正在运行,又可以避免无鸡的地方。

  • Use VSCode or your favorite IDE to do a step-by-step evaluation of your function, to know exactly what is happening.使用 VSCode 或您最喜欢的 IDE 对您的 function 进行逐步评估,以准确了解发生了什么。 Barring that, add Debug.Log s for each step.除此之外,为每个步骤添加Debug.Log

  • Check the hierarchy for any spawned chickens.检查任何生成的鸡的层次结构。 Maybe they are disabled.也许他们是残疾人。

Now for other issues:现在讨论其他问题:

  • Please follow a C# convention for writing code.请遵循 C# 约定编写代码。 Chkn and Loc are about as unintuitive as it gets. Chkn 和 Loc 几乎是不直观的。

  • If you ignore the z position when evaluating distances, please consider using Vector2.Distance.如果在评估距离时忽略 z position,请考虑使用 Vector2.Distance。

  • Using a field for a value that you only use inside one function is useless.将字段用于仅在 function 内部使用的值是没有用的。 Simply set spawnLoc as a var inside ChickenInstantiate .只需将ChickenInstantiate设置为spawnLoc中的 var。

Here's a cleaned up version of your code这是您的代码的清理版本

void InstantiateChicken()
{
    List<Vector3> chickenPositions = new List<Vector3>();

    for (int i = 0; i < maxChickenCount; i++)
    {
        bool doesCollide;
        Vector3 spawnLocation;
        do
        {
            spawnLocation = new Vector3(
                Random.Range(randXMin, randXMax),
                Random.Range(randYMin, randYMax),
                5);
            doesCollide = false;
            foreach (var pos in chickPositions)
            {
                if (Vector2.Distance(pos, spawnLocation) < 2f)
                    doesCollide = true;
            }
        }
        while (doesCollide);

        Instantiate(chickenPrefab1, spawnLocation, Quaternion.identity);
        chickenPositions.Add(spawnLocation);
    }
}

Alternatively, simply place all your chickens by hand, and make them offset their positions by a random factor when they spawn.或者,只需手动放置所有鸡,并让它们在产卵时以随机因子偏移它们的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM