Question by
unity_HmvY27SkgCLS1Q · May 11, 2019 at 01:47 AM ·
prefab2d game2d-platformerspawnspeed
How to spawn prefabs with the same speed as the prefabs spawned before?
So im working on a 2d endless runner game. I want to increase my game speed over time and as you can see in the picture im spawning platforms before spawning obstacles and this will cause prefabs (platforms and obstacles) to have different speed. How can i make this? Any help would be appreciated.
Obstacle Spawner Script:
public GameObject[] obj; public GameObject bomba; public GameObject platform; public float spawnMin = 1f; public float spawnMax = 2f; public Vector3 pos = new Vector3(-4, 0, 0); public float size = 1.0f; private Vector3 dir = Vector3.right;
void Start()
{
StartCoroutine(Spawn());
}
IEnumerator Spawn()
{
while (true)
{
GameObject myobj = Instantiate(obj[Random.Range(0, obj.Length)], pos, Quaternion.identity) as GameObject;
myobj.transform.parent = platform.transform;
pos += dir * size;
yield return new WaitForSeconds(Random.Range(spawnMin, spawnMax));
}
}
}
Object Movement:
public float speed = 11f;
public float maxSpeed = 18f;
public float accelerationTime = 10;
private float minSpeed;
private float time;
void Start () {
minSpeed = speed;
time = 0;
}
void Update () {
transform.Translate (-speed * Time.deltaTime, 0,0);
speed = Mathf.SmoothStep(minSpeed, maxSpeed, time / accelerationTime);
// transform.position -= transform.forward * speed * Time.deltaTime;
time += Time.deltaTime;
}
}
Platform Spawner:
public GameObject platform;
public Transform generationPoint;
public float distanceBetween;
private float platformWidth;
public float distanceBetweenMin;
public float distanceBetweenMax;
// Use this for initialization
void Start () {
platformWidth = platform.GetComponent<BoxCollider2D>().size.x;
}
// Update is called once per frame
void Update () {
if(transform.position.x < generationPoint.position.x)
{
distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, -11.79f, transform.position.z);
Instantiate(platform, transform.position, transform.rotation);
}
}
}
increasespeed.png
(14.7 kB)
Comment