- Home /
Continuous instantiation of a moving prefab
Hello, I'm trying to create a simple runner game, and I decided to create prefabs of segments and move them towards the player, while the player is only animating the running.
The idea is to instantiate a new segment of the tunnel, and when it passes the player, to remove it... and continue doing so endlessly.
My problem is that I don't know how to make sure the segments are instantiating right next to each other, so there won't be a gap and no overlapping.
Currently, I'm using an InvokeRepeating on the function that will instantiate a prefab (with the script that moves it already attached). But I'm getting inconsistent gaps between the prefabs.. I'm guessing it's because the InvokeRepeating method doesn't use the deltaTime or something of that nature... and I think that it's generally not the best practice for this kind of thing (correct me if I'm wrong).
I thought about checking the position of the prefab, and when it reaches a certain point then I will instantiate a new segment, but this can also cause inconsistent gaps, I think, since the prefab can move move more than one point if there is a lag, right? and I need the new segment to "attach" itself to the previous segment.
I also made an illustration to explain it more visually.
It's a Side View of what's going on. The one pixel gap between segments is just to make it possible to see them.
Can anyone point me in the right direction as to how to generally do this sort of thing with the position precision I need?
Thanks!
Answer by Nicolinux · Jan 21, 2012 at 12:15 AM
You need to take the width of your segments into account and define a point (offscreen) where you want the replacement to appear. Then in Update() simply check the transform.position.x if it is less then your defined point. If it is, then move your segment there via transform.position = ... Here is an example. I have defined leftSide as the outer most point and rightSide as the right most point:
var speed : float; var leftSide: float; var rightSide: float;
function Update () {
var move : float = speed Time.deltaTime; transform.Translate(Vector3.left move, Space.World);
if (transform.position.x < leftSide) { transform.position = Vector3(rightSide, transform.position.y, transform.position.z); }
}
Answer by Rick.Miranda · Sep 15, 2013 at 04:04 PM
Hi, I see this was posted quite a while back. But I've been dealing with this problem myself. I've been needing to generate scrolling background and was also having the issue with the gap (or seam) between instances. Not sure how you are implementing this. But I will explain the way I am implementing. I start off with two background objects stacked vertically. They scroll downwards. Each background object has the script attached that checks the current position. If the position is the final position, will create a new background object on top of the second background object, and the point is to do it so that it is seamless. The check for this position was written as: if (transform.position <= final position). Then I would place the new object a fixed distance from the second object. But this distance was fixed and did not respond to any position error. So if it so happened that the object position was less than the final, there was an error amount associated with the placement of the new object. So the way I corrected this was by calculating the error, then the placement would be the fixed amount (based on the length of the tile) minus the error. This worked charmingly.
I'm sure by now you have moved on to another project or have solved it but if you need the code let me know.
Right now, I'm trying to figure out how to do the same procedure but now with different prefabs to give the appearance of changing background, like driving a car over changing landscape. Up to now, I've been using transform.position = new Vector 3( ) to generate the new prefab but since now I want to access other prefabs in the assets folder, I'm trying to use "Instantiate( )". But can't figure out why this is causing the prefabs to clone back to back instead of only when the object position reaches final.