- Home /
Sprite generation
Hi!
I am making an infinite runner game. I have made a very simple script which makes the ground move to the left (making it look like the character is running to the right, when hes actually just running on the spot). When the ground sprite has moved out of the camera view, the character falls of the world (because there is no ground)
I would need some guidance to make my script:
Destroy the ground sprite when it has passed the camera and
Generate the ground sprite at the starting position when it has been destroyed, continiously.
I cant make an infinite runner with a finite amount of ground sprites!
The GroundMovementCode:
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (Vector3.left * 5 * Time.deltaTime);
}
}
Thanks in advance and have a nice day!
Depending on what you mean by "ground sprite", there could be multiple ways to handle this. You actually can make an infinite running with a finite number of ground sprites. You just need to have enough of them offscreen to handle rotating them back into the mix. What I mean is you would move the transform of the sprite from the left side of the screen to the right side of the screen once it goes off the left side. It would then start just off screen to the right side and scroll back in. You don't want to destroy and re-create them. That's very inefficient.
Answer by kevinspawner · Mar 24, 2015 at 06:55 AM
public float scrollSpeed;
public float tileSizeX;
private Vector3 startPosition;
void Start ()
{
startPosition = transform.position;
}
void Update ()
{
float newPosition = Mathf.Repeat(Time.time*scrollSpeed,tileSizeX);
transform.position = startPosition + Vector3.right*newPosition;
}
Just add this script in your sprite and it will repeat the sprites once it reach the end, So this way you will be looping infinitely. If you have more than one sprite than need to be looped simply create an empty game object and drag the multiple sprites into the empty game object and drop this script in to the parent [empty game object].
Note: Change the scrollspeed and tilesize properties according to your need.