- Home /
Dynamically Repeat plane to possible infinity
I am creating a 2D game and in order to create a good sense of movement I am using an image projected on a plane that acts as a backdrop. However, the game could go on for an infinite amount of time as it is a flying-side-scroller, thus I need the backdrop to repeat to infinity dynamically. Now obviously I cant just tell the computer to repeat it to infinity in the onStart() as this would crash everything, so I need to do it dynamically. This means I need to repeat it as the user nears the edge of the plane. I have several attempts but the plane always spawns in the wrong place and I can not figure it out. A skybox will not work as it does not create the sense of movement I need.
Read the comments, they explain quite a bit of stuff Thanks for reading. Hope it wasn't a book!
------------------------Code-------------------
var planeWidth: int;
var plane : Transform;
var player : Transform;
var middle : Transform;
var right : Transform;
function Update () {
Debug.DrawRay(Vector3(player.position.x,player.position.y,player.position.z), Vector3(plane.position.x + (1500), plane.position.y,plane.position.z), Color.blue);
//1500 is the plane width?? I dont know was a shot in the dark.
if(Physics.Raycast(Vector3(player.position.x,player.position.y,player.position.z), Vector3(plane.position.x + (1500), plane.position.y,plane.position.z),100)){
Debug.Log("Something in the way :)");
//ray cast tells me if the plane is still there. Raycast at an angle so the user never sees the end of the plane and the new plane is there before the user hits that position.
}else{
//spawn the plane
Debug.Log("Need to draw new backdrop");
planeWidth = Vector3.Distance(middle.position, right.position);
//get the width of the plane. two empties in the middle and right and get the distance between them, there is where the plane needs be spawned.
Instantiate(plane, Vector3 (plane.position.x + planeWidth, plane.position.y, plane.position.z), Quaternion.identity);
//should work unless I missed something stupid, or am way off track lol. Thanks guys!
}
}
It looks like you have the right idea, what exactly is the problem? Is the new plane spawning at the wrong position and if so, is it slightly off on the x, y or z axis or is it spawning in the wrong area altogether? thanks
Its spawning at a weird rotation and I cant figure out how to rotate it to be parallel to the plane....
Is this script on the camera object? If it is then you could try replacing the line:
Instantiate(plane, Vector3 (plane.position.x + planeWidth, plane.position.y, plane.position.z), Quaternion.identity);
with:
Instantiate(plane, Vector3 (plane.position.x + planeWidth, plane.position.y, plane.position.z), plane.rotation);
I'm guessing that the plane variable in your script is set to the transform of the background.. Basically try replacing Quaternion.identity with the actual rotation of the background ;)
well, I feel stupid! LOL. How did i miss that :) thank you so much! You should write that as an answer so i can accept it and you get credit where credit is due
Answer by ByteSheep · Mar 18, 2012 at 04:27 AM
Answer in the comments, haha thanks..
Hi There,
$$anonymous$$ay i know how it works exactly?
Regards, $$anonymous$$ranthi