- Home /
Instantiating road pieces without any spacings between
Hello!I`m tring to build an infinite road out of different pieces,each road piece moves on the z axis.Basically i want to instantiate a new piece when the prevoius one has reached a certain position in z axis without any spaces between them . I have tried alot of methods but none seems to work right .Mainly i noticed that the update function reads the position a bit late since the road piece moves more space units per frame, and the faster the road moves the more inaccurate it's transform.position.z is read.I really need some help on this since i've been struggling for days to solve it with no luck.
Answer by Vonni · Jan 08, 2013 at 09:41 AM
Assuming you have a lot of different roads:
You should create a joint (empty gameObject) on the end of the road that you can use to snap to. And then have the pivot of every road prefab at the other end.
And then in the script you should Instantiate it, parent it and then move it to the joint. Maybe have one master controller that controls the speed of the road and parent everything to that.
Answer by Dan.R · Jan 08, 2013 at 12:37 PM
Thanks Vonni ! I didnt know about jointHinges this works great.So basically i added an empty with rigidbody and a box collider with the length of the road piece, I positioned it under the road so it doesnt collide with other objects on the road,added the road piece as a child of the empty,set the speed of the initial piece to 50 and the next instantiated one to 70 so it outruns and then snaps to the first piece and then on collision enter changed its speed to 50 again to slow it down for the next instantiate. This is the script i used:
var roadSpeed : float = 70;
function Update ()
{
transform.Translate(0, 0,roadSpeed*Time.deltaTime);
if (gameObject.transform.position.z >=200)
{
Destroy(gameObject);
}
}
function OnCollisionEnter(c : Collision) {
var joint = gameObject.AddComponent(FixedJoint);
joint.connectedBody = c.rigidbody;
roadSpeed=50;
}
This was not what I meant, but glad you found a way :) Why are you using rigidbodies?
Your answer
Follow this Question
Related Questions
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
How can I get a GameObject's transform in game and set that to a variable? 1 Answer
Vector3.Lerp works outside of Update() 3 Answers
Use GameObject's global position instead of local position 3 Answers
How do I get the opposite position of an object that spins around another object... 0 Answers