给每个预制件自己的速度
Give every prefab his own speed
我正在为 UNITY3D 中的太空射击游戏构建星空背景模拟。
在 C# 中,我构建了看起来像点的预制件的实例化。虽然在 x 轴上的随机点上生成多个点非常完美,但我想添加一件事。
随机速度。问题是我不知道如何给每个预制件他自己的速度并保持它而不是被下一个随机化覆盖。
backgroundloop.cs
using UnityEngine;
using System.Collections;
public class backgroundloop : MonoBehaviour {
public GameObject star;
public float spawnTime;
private GameObject starPrefab;
private float timestamp;
//public float hoogte = Random.Range(-1.01f,1.1f);
//public float rotationPrefab = transform.localEulerAngles.z;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Time.time > timestamp) {
starPrefab = Instantiate
(star, new Vector3(7,Random.Range(-5.0f,5.0f),0),
Quaternion.Euler(0, 0, 0)) as GameObject;
timestamp = Time.time + spawnTime;
}
}
}
问题出在这个脚本中:
prefabMovement.cs
using UnityEngine;
using System.Collections;
public class prefabMovement : MonoBehaviour {
float speed = Random.Range (-3f, -0.1f);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(-1f,0,0) * Time.deltaTime);
if (transform.position.x < -6.8) {
Destroy (this.gameObject);
}
}
}
嗯?所以速度被覆盖并最终在所有实例化的预制件上都相同?
如果在 Start 方法中初始化速度变量可能会有所帮助。
我正在为 UNITY3D 中的太空射击游戏构建星空背景模拟。 在 C# 中,我构建了看起来像点的预制件的实例化。虽然在 x 轴上的随机点上生成多个点非常完美,但我想添加一件事。
随机速度。问题是我不知道如何给每个预制件他自己的速度并保持它而不是被下一个随机化覆盖。
backgroundloop.cs
using UnityEngine;
using System.Collections;
public class backgroundloop : MonoBehaviour {
public GameObject star;
public float spawnTime;
private GameObject starPrefab;
private float timestamp;
//public float hoogte = Random.Range(-1.01f,1.1f);
//public float rotationPrefab = transform.localEulerAngles.z;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Time.time > timestamp) {
starPrefab = Instantiate
(star, new Vector3(7,Random.Range(-5.0f,5.0f),0),
Quaternion.Euler(0, 0, 0)) as GameObject;
timestamp = Time.time + spawnTime;
}
}
}
问题出在这个脚本中: prefabMovement.cs
using UnityEngine;
using System.Collections;
public class prefabMovement : MonoBehaviour {
float speed = Random.Range (-3f, -0.1f);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector3(-1f,0,0) * Time.deltaTime);
if (transform.position.x < -6.8) {
Destroy (this.gameObject);
}
}
}
嗯?所以速度被覆盖并最终在所有实例化的预制件上都相同?
如果在 Start 方法中初始化速度变量可能会有所帮助。