- Home /
Spawn another platform infront of the original platform
Hi everyone, I'm new to C# unity programming and I'm learning to create a 3D endless straight running platform track but instead of the player moving, I'll want to make the platform move instead. However the only problem I'm facing is spawning another platform infront of the original platform when the original platform moves out of the camera view or a certain axis.
This is my current code to make the platform move and I would like to spawn another platform infront of the original platform once it goes out of camera view.
Forgive my simple coding as I'm relatively new to programming.
using UnityEngine;
using System.Collections;
public class Platform : MonoBehaviour {
public float speed = 6f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.back * Time.deltaTime * speed);
}
}
Are all the platforms going to be the same? If so I would recommend just having two platforms and moving the last in front of the first after it goes out of view. This would cut down on object creation. There is actually a pretty good example of this in the scrolling backgrounds live training.
Thanks a lot for the tutorial and yes, they are going to be the same however I can't seem to be able to move the first platform which went out of the camera screen first to be moved back in front of the second platform which is about to go off screen.
Answer by Franky_Cranky_Man · Jul 22, 2014 at 04:00 AM
Hi Raddish,
Your code right now takes a platform and moves it. It does not spawn any new objects. Here is how you could go about it. Apologize for any typos as I am not using an IDE to make this code.
public float speed;
// Prefab of the platform you want to create
public GameObject platformPrefab;
// Reference to the last platform you created
private GameObject lastCreatedPlatform;
// Stored position of the last platform
private Vector3 lastPlatformPosition;
//Position offset of each platform
public Vector3 offset;
void Awake()
{
speed = 6f;
lastPlatformPosition = new Vector3(0,0,0);
offset = new Vector3(1,0,0);
{
void Start()
{
// Runs CreatePlatform() every second
InvokeRepeating("CreatePlatform", 0, 1f);
}
CreatePlatform()
{
lastCreatedPlatform = (GameObject)Instantiate(platformPrefab, lastPlatformPosition + offset, Quaternion.Identity);
lastPlatformPosition = lastCreatedPlatform.transform.position;
}
Thanks a lot for your time and help Franky_Cranky_$$anonymous$$an but I can't seem to be able to spawn the platform in the Z axis as it is spawning on the X axis ins$$anonymous$$d and is it possible for me to reduce the gap in between the platforms? As the platform tend to be spawn quite a distance from the original platform. Either way, really appreciate your help a lot. Thank you for your time once again was really able to understand what you had written easily. Hope I can get better
It's spawning in the x axis because of this line:
offset = new Vector3(1,0,0);
Just modify this vector to whatever direction(s) you want it to move in like offset = new Vector3(0,0,1);
Your answer
Follow this Question
Related Questions
Physics platform character controller sliding issue NO parenting 0 Answers
Player falling through the floor on running? 1 Answer
[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 Answers
Most efficient way to create/spawn a lot of enemies at the same time(Unity3D) 1 Answer
3dplatformer error with windows7 x86 and webplayer 0 Answers