【问题标题】:Gameobject that moves and spawns prefabs移动和生成预制件的游戏对象
【发布时间】:2015-02-02 08:01:01
【问题描述】:

我有一个游戏对象,它向前移动并每隔一秒左右生成一个预制件。唯一的问题是产卵似乎不均匀,有时物体相距很远,有时它们重叠。我可以通过改变生成时间和距离来解决这个问题,但是当我在 ios 上测试它时它又坏了。

我尝试创建一个脚本,该脚本将游戏对象推出另一个对象,但它推出了,然后对象击中另一个对象,然后以另一种方式。

我想要做的是让生成器向前移动并向下放置一个预制件,这样预制件就不会重叠。我需要这个在 ios 上工作。我认为它可能是由于

这里是生成脚本:

using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {

    public GameObject[] obj;
    //public float spawnMin = 1;
    //public float spawnMax = 1;
    //public float spawnDistance = 0.1f;
    //private float barDisplay = 3; 
    public float timeLeft = 0.1f;  
    static bool finishTimer = false; //i made this global so you can access from other scripts 
    static bool timerStarted = false;

    //private GameObject GroundSpawner;

    // Use this for initialization
    void Start () {
        timerStarted = true;

    }

    void Update(){
        Timer ();

        transform.Translate(Vector3.forward * 0.1f);


    }

    void Timer(){

        if (timerStarted == true) {         
            timeLeft -= Time.deltaTime;          
            if (timeLeft <= 0.0f) {        
                Spawn ();
                timerStarted = false;
            }
        }
    }

    void Spawn(){
        //GroundSpawner = GameObject.Find ("Ground Spawner");

        //if (GroundSpawner.transform.position) {
                    //Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
                    Invoke ("Spawn", 1.75f);

                    Instantiate (obj [Random.Range (0, obj.Length)], transform.position, Quaternion.identity);

        //  }
    }
}

这是另一个游戏对象脚本推出的基础:

using UnityEngine;
using System.Collections;

public class GroundHittingSelf : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

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

    }

    void OnCollisionEnter(Collision other) {
        Debug.Log("Hitting Ground without tag");
        if (other.gameObject.tag == "Ground") {
            //transform.position += new Vector3(0, 0, 0.1f);
            Debug.Log("Hitting Ground with tag");

        }
    }
}

【问题讨论】:

  • 好吧,无法准确理解您要做什么。您可以发布任何带有您的问题的图片(屏幕截图)吗?

标签: c# ios unity3d


【解决方案1】:

实例化预制件之间的不均匀空间来自于您的带有SpawnScript 的游戏对象正在随着速度变化而移动。

这是因为您在 Update 函数中使用 transform.Translate 更改位置,而没有考虑 deltatime。 Update 一秒钟内被调用的次数与您运行代码的机器能够调用它的次数一样多。例如,如果机器正在做一些后台工作,Update 的调用频率就会降低。在这种情况下,游戏对象会变慢。或者更糟糕的是,如果在不同的机器上运行相同的代码,它会更快/更慢。

这可以通过考虑 deltatime 来解决:

transform.Translate(Vector3.forward * Time.deltaTime * 3.0f);

Deltatime 通常是一个很小的数字,因此需要重新设置常量。例如,如果您的 30fps 速度不错,那么 3.0f 就是其中之一,因为 3.0f * (1.0f / 30.0f) = 0.1f

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多