- Home /
Instantiate leaving a gap [Endless Runner]
Hello. I'm trying to create endless runner by moving gameobjects under my player instead of actually moving the player.
Everything works fine except every 4th instantiate creates a gap which size varies depending of the speed I'm moving the objects. I have been trying to fix this for a week now and I'm getting kinda depressed about it. Now, I understand I'm destroying the GO after a specific cordinates, -3,5 on Z axis to be specific, and I understand that those numbers won't round perfectly. So sometimes its destroyed on -3,5xxxx and I'm sure thats what could be cousing the problem. But I have no idea how to fix it or some other workaround.
Image:
using UnityEngine;
using System.Collections;
public class CubeBehaviour : MonoBehaviour {
public Vector3 speed = new Vector3(0, 0, -20);
public GameObject prefab;
private Vector3 pos; // position of rigidbody
private float length; // length of object
// Use this for initialization
void Start () {
// copy our object's default rigidbody position
pos = rigidbody.position;
// fetch the object's length. we'll use this to determine
// if we've past our camera...
length = 3.5f;
}
// Update is called once per frame
void FixedUpdate () {
// move our z towards the camera
// to do this, we subject our position by the speed
pos.z = rigidbody.position.z;
// let's update our rigidbody's position
// in this exercise, we're just moving the z. however, we
// have cached the X and Y in our start function.
rigidbody.MovePosition(rigidbody.position + speed * Time.deltaTime);
// if our object's length is past our camera (0,0,0), then...
if (pos.z < -length)
{
// delete our object
Instantiate(prefab, rigidbody.position + new Vector3(0,0,28f), transform.rotation);
Destroy(gameObject);
}
}
}
does it work ok if you move the player AFTER you instantiate the new object? if this does, then you can implement another piece of code to destroy the object
Im not moving the player on z axis at all. Only changing its x and y position.
Answer by Turkeybag · Oct 10, 2014 at 01:26 PM
I would suggest storing the last object spawned and using it's length value combined with the object your about to spawns length value to adjust the position of the next object spawned so you get pinpoint accuracy. Something like this:
Instantiate(prefab, LastSpawn.position + new Vector3(0,0,(LastSpawn.Length + AboutToSpawn.Length) /2), transform.rotation);
It's just a base and you need to access the length variable and what not but hopefully that will put you on the right track and good luck!
As you can see in the image above im using the objects of the same length, so your code would still be the same as $$anonymous$$e. Nothing is wrong with my code either, it does what I want but sometimes creates that gap. I guess its some kind of float precision error. I'm looking for a way around this.
Oh I thought you were only using the length to destroy the last object rather than create the new one. Hmm. A workaround could be to move the position in the start function of the object once you spawn it.
Answer by Enes22 · Dec 09, 2015 at 04:13 PM
I know this tread is one year old, but had the same problem. Fixed with setting Interpolate to Interpolate on rigidbody component. So whoever have same problem try my solution.
Answer by lvrbrtsn · Jan 02, 2020 at 05:01 AM
I was running into this same issue and found out it had to do with script execution order. My script that determined if it should spawn a road ran before the road movement function. This caused it so have a stale reference to the position by the time Unity actually Instantiates the object.
You can fix this by adding and moving the script that moves your environment to before default time in Project settings > Script Execution Order.
Your answer
Follow this Question
Related Questions
Instantiate objects in a specific order 2 Answers
Generate a continuos platform for endless runner 3 Answers
instanstiate Spawns more then one objec 0 Answers
How to use the same object more than once at the same time when using an "object pooling" pattern? 3 Answers
Trouble spawning endless platform 0 Answers