- Home /
Making randomly generated platforms translate
I've been working through a tutorial on catlikecoding.com and made the "Runner" game. Now I'm trying to play around with it to understand it better. It uses a platform manager to generate and destroy the platforms when they move off screen. I'd like the platform to move towards the player and destroy itself as usual but I cant get it to work. Should I put the transform code in the platform manager script or its own script? Heres the PlatformManager script for reference.
using UnityEngine;
using System.Collections.Generic;
public class PlatformManager : MonoBehaviour {
public Transform prefab;
public int numberOfObjects;
public float recycleOffset;
public Vector3 minSize, maxSize, minGap, maxGap;
public float minY, maxY, maxZ, minZ;
public Material[] materials;
public PhysicMaterial[] physicMaterials;
public Booster booster;
public float movementSpeed;
private Vector3 nextPosition;
private Queue<Transform> objectQueue;
void Start () {
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
objectQueue = new Queue<Transform>(numberOfObjects);
for(int i = 0; i < numberOfObjects; i++){
objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f, 0f, -100f), Quaternion.identity));
}
enabled = false;
}
void Update () {
if(objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled){
Recycle();
////HERE is where I try to get the to move
transform.position = new Vector3(movementSpeed * Time.deltaTime, transform.position.y, transform.position.z);
////END
}
}
private void Recycle () {
Vector3 scale = new Vector3(
Random.Range(minSize.x, maxSize.x),
Random.Range(minSize.y, maxSize.y),
Random.Range(minSize.z, maxSize.z));
Vector3 position = nextPosition;
booster.SpawnIfAvailable(position);
Transform o = objectQueue.Dequeue();
o.localScale = scale;
o.localPosition = position;
int materialIndex = Random.Range(0, materials.Length);
o.renderer.material = materials[materialIndex];
o.collider.material = physicMaterials[materialIndex];
objectQueue.Enqueue(o);
nextPosition += new Vector3(
Random.Range(minGap.x, maxGap.x) + scale.x,
Random.Range(minGap.y, maxGap.y),
Random.Range(minGap.z, maxGap.z) + scale.z);
if(nextPosition.y < minY){
nextPosition.y = minY + maxGap.y;
}
else if(nextPosition.y > maxY){
nextPosition.y = maxY - maxGap.y;
}
if(nextPosition.z < minZ){
nextPosition.z = minZ - maxGap.z;
}
else if(nextPosition.z > maxZ){
nextPosition.z = maxZ + maxGap.z;
}
}
private void GameStart () {
nextPosition = transform.localPosition;
for(int i = 0; i < numberOfObjects; i++){
Recycle();
}
enabled = true;
}
private void GameOver () {
enabled = false;
}
}
Am I even doing it correctly? Any help or guidance is much appreciated as Im not to knowledgeable about this as I'm only starting out. Thanks!
You can Instantiate the platforms using the Platform $$anonymous$$anager and the Platform prefab can have its own script that transforms the platform. Inside this script you can check for collision or other events and destroy it then. What part is not working correctly?
Should I be putting my transform in its own script or should I put it in the platform manager?
Your answer
Follow this Question
Related Questions
Platform does not move accurate. Transform translate not accurate. 1 Answer
move enemy to random transform array 2 Answers
2D runner, need a help 0 Answers