移动和生成预制件的游戏对象
Gameobject that moves and spawns prefabs
我有一个游戏对象,它向前移动并每隔一秒左右生成一个预制件。唯一的问题是产卵似乎不均匀,有时物体相距很远,有时它们重叠。我可以通过更改生成时间和距离来解决这个问题,但是当我在 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");
}
}
}
实例化预制件之间的不均匀空间是因为您的 SpawnScript
游戏对象随着速度的变化而移动。
这是因为您在 Update
函数中使用 transform.Translate
更改位置,而没有考虑增量时间。 Update
一秒钟内被调用的次数与您 运行 正在运行代码的机器能够调用它的次数一样多。例如,如果机器正在做一些后台工作,Update
被调用的频率就会降低。在那种情况下,游戏对象会变慢。或者更糟糕的是,如果相同的代码在不同的机器上是 运行 它将是很多 faster/slower.
这可以通过考虑增量时间来解决:
transform.Translate(Vector3.forward * Time.deltaTime * 3.0f);
Deltatime 通常是一个很小的数字,因此需要重新设置常量。例如,如果你有 30fps 的速度,那么 3.0f 就是那个,因为 3.0f * (1.0f / 30.0f) = 0.1f
我有一个游戏对象,它向前移动并每隔一秒左右生成一个预制件。唯一的问题是产卵似乎不均匀,有时物体相距很远,有时它们重叠。我可以通过更改生成时间和距离来解决这个问题,但是当我在 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");
}
}
}
实例化预制件之间的不均匀空间是因为您的 SpawnScript
游戏对象随着速度的变化而移动。
这是因为您在 Update
函数中使用 transform.Translate
更改位置,而没有考虑增量时间。 Update
一秒钟内被调用的次数与您 运行 正在运行代码的机器能够调用它的次数一样多。例如,如果机器正在做一些后台工作,Update
被调用的频率就会降低。在那种情况下,游戏对象会变慢。或者更糟糕的是,如果相同的代码在不同的机器上是 运行 它将是很多 faster/slower.
这可以通过考虑增量时间来解决:
transform.Translate(Vector3.forward * Time.deltaTime * 3.0f);
Deltatime 通常是一个很小的数字,因此需要重新设置常量。例如,如果你有 30fps 的速度,那么 3.0f 就是那个,因为 3.0f * (1.0f / 30.0f) = 0.1f