变量一次设置后随机更改

时间:2019-03-23 09:31:08

标签: c# unity3d

每当我在地图上随机生成的位置删除一个GameObject时,我都会尝试生成它。

我有以下代码来产生食物

using UnityEngine;

public class SpawnManager : MonoBehaviour
{

    public GameObject food;

    private Vector3 maxSpawnLimit;
    private Vector3 minSpawnLimit;

    private Vector3 spawnLocation;
    private Quaternion spawnRotation;

    private GameObject ground;

    private void Start()
    {
        ground = GameObject.Find("Ground");

        maxSpawnLimit = ground.transform.lossyScale / 2 + new Vector3(-1, 1, -1);
        minSpawnLimit = -ground.transform.lossyScale / 2 + new Vector3(1, 1, 1);

        Debug.Log("Start Spawn limit " + maxSpawnLimit + minSpawnLimit);
    }

    public void SpawnFood()
    {
        spawnLocation = new Vector3(Random.Range(minSpawnLimit[0], maxSpawnLimit[0]), 0, Random.Range(minSpawnLimit[2], maxSpawnLimit[2])) + new Vector3(0, 5, 0);
        spawnRotation = Quaternion.Euler(0, 0, 0);
        Instantiate(food, spawnLocation, spawnRotation);
        Debug.Log("Spawn Limits " + maxSpawnLimit + minSpawnLimit);
    }
}

在开始方法中设置了最大和最小生成限制时,我运行了Debug.Log,尽管我在SpawnFood方法中做了同样的设置,并且从{{1}开始更改,但它们没有变化},应将它们添加到(49, 1, 49),直到产生第一批食物。为什么会这样?

编辑: (0, 0, 0)脚本附加到https://i.imgur.com/XxbupJj.png

的对象的检查器

该脚本显示了碰撞检测以及随后的方法调用和对象删除

SpawnManager

0 个答案:

没有答案