首页 文章

为什么函数第二次不被调用?

提问于
浏览
0

我试图在游戏中根据当前的玩家位置产生一些GameObjects,基本上试图制作一个无限的跑步者类型的游戏...我做了一个函数,所以我可以用它生成塔,但由于某种原因,该函数只被调用每帧一次,不会使用不同的参数第二次调用它 .

为什么函数的第二次调用不起作用?

这是我的代码:

public class CameraScript : MonoBehaviour {
    public float cameraSpeed = 1;
    public float horizontalSpeed;
    private int spawnIndex;
    public float spawnNormPylonDis;
    public float spawnCoinPylonDis;
    private int currPosition ;
    public GameObject[] pilons;
    public GameObject spawnMainPoint;
    public Transform[] spawnPoints;
    public Transform[] coinsSpawnPoint;
    public float enamySpeed;
    private int currentPoint;
    public Transform[] pointPosition;

    // Use this for initialization
    void Start () {
        //This spawns the Pilons.
        spawnMainPoint.transform.position = pointPosition [0].position;
        currPosition = (int) transform.position.z;
    }

    // Update is called once per frame
    void FixedUpdate () {

    spawnMainPoint.transform.position = Vector3.MoveTowards (spawnMainPoint.transform.position, pointPosition[currentPoint].position, Time.deltaTime * horizontalSpeed);

    SpawnPylon (pilons[1],spawnPoints,spawnNormPylonDis,"Check function");
    SpawnPylon (pilons [0], spawnPoints, spawnCoinPylonDis,"Check the second function");

    GetComponent<Rigidbody> ().velocity = transform.forward * cameraSpeed;

    //the next if statements make the Pilons spawn randomly and corectly.
    if (spawnMainPoint.transform.position == pointPosition [currentPoint].position) {
        currentPoint++;
    }
    if (currentPoint == pointPosition.Length) {
        currentPoint = 0;
    }

}
/*This function spanws the a GameObject randomly at a GameObject's position and it takes 2 arguments :
Argument 1: type GameObject 
         2: type Transform[]*/
void SpawnPylon (GameObject whatSpawn,Transform[] whereSpawn,float spawnDistance,string plm)
{       
    bool hasSpawnedPylon = false;

    if (currPosition != (int)transform.position.z)
    {
        if ((int)transform.position.z % spawnDistance == 0) 
        {
            Debug.Log (plm);
            if (!hasSpawnedPylon) 
            {
                //this makes the GameObject spawn randomly
                spawnIndex = Random.Range (0, spawnPoints.Length);
                //This is instantiationg the GameObject
                Instantiate (whatSpawn, whereSpawn [spawnIndex].position, whereSpawn [spawnIndex].rotation);
                //this makes shore that the GameObject is not spawned multiple times at aproximetley the same position.
                currPosition = (int)transform.position.z;
            }
        }
    }
    else
    {
        hasSpawnedPylon = false;
    }           
}
}

在这里,我在检查器中有一个带脚本的图片:

enter image description here

脚本检查员

这是控制台,试图通过使用Debug.Log()来解决该函数的调用问题 .

enter image description here

使用Debug.Log进行调用 .

1 回答

  • 1

    我测试了它,在某些条件下工作正常 . 最有可能的问题是你的spawnNormPylonDis和spawnCoinPylonDis是检查器中相同的公共变量,或者spawnNormPylonDis是spawnCoinPylonDis的倍数,几乎与你的代码一样,你不能让spawnNormPylonD为1.如果它们是那么在SpawnPylon函数中因为它已经产生了一个 . 你自己评论道 .

    //this makes shore that the GameObject is not spawned multiple times at aproximetley the same position.
    currPosition = (int)transform.position.z;
    

    我测试了这个值为2和3并且两个函数都被调用了,但是,如果它们都可以被整除,它将跳过第二个函数(6) .

    因此,您在这行代码中遇到了瓶颈,因为您在第一次函数调用后设置了currPosition .

    if (currPosition != (int)transform.position.z)
    

    通过制作一个基本的包装功能,一切都在世界上很好!所以我们在检查了两个产卵调用之后设置了currPosition,并且问题解决了 .

    void SpawnPylon(int index, Transform[] spawnPoint, float dist, string debug)
    {
        if ((int)transform.position.z % dist == 0)
        {
            //Debug.Log(debug);
            //this makes the GameObject spawn randomly
            spawnIndex = Random.Range(0, spawnPoints.Length);
            //This is instantiationg the GameObject
            GameObject go = Instantiate(pilons[index], spawnPoint[spawnIndex].position, spawnPoint[spawnIndex].rotation) as GameObject;
            go.name = string.Format("ID: {0}", index);
        }
    }
    
    private void SpawnMultiplePylons()
    {
        if (currPosition != (int)transform.position.z)
        {
            SpawnPylon(1, spawnPoints, spawnNormPylonDis, "Spawn1");
            SpawnPylon(0, spawnPoints, spawnCoinPylonDis, "Spawn2");
            currPosition = (int)transform.position.z;
        }
    }
    

    我测试了这个并且它有效 . 我希望它也适合你!

相关问题