- Home /
how t make a random number generator for a 2d endless side scroller
I have tried for so long and I have got this far to figure this much out can any one help me using the script that I provide public class PlatformManager : MonoBehaviour { public int startingNumber; public List<string> levelComponents; 5. // define it as the height on one or more component objects (The actual value you will need to find out) public float componentBuffer; public float screenBuffer; public List<Transform> platformList; 10. private Vector3 nextPosition; private Transform player; void Start() { 15. nextPosition = new Vector3(0.0f, -componentBuffer, 0.0f); platformList = new List<Transform>(); for (int i = 0; i < startingNumber; i++) { int randComponent = Random.Range(0, levelComponents.Count); 20. GameObject newPlatform = PoolManager.Spawn(levelComponents[randComponent]); newPlatform.transform.position = nextPosition; platformList.Add(newPlatform.transform); nextPosition.y -= componentBuffer; } 25. } void Update() { if (player == null) 30. { player = GameObject.Find("Player(Clone)").transform; } else { 35. if (nextPosition.y > player.position.y - screenBuffer) { int randComponent = Random.Range(0, levelComponents.Count); GameObject newPlatform = PoolManager.Spawn(levelComponents[randComponent]); newPlatform.transform.position = nextPosition; 40. platformList.Add(newPlatform.transform); nextPosition.y -= componentBuffer; } if (platformList[0].position.y > player.position.y + screenBuffer) 45. { PoolManager.Despawn(platformList[0].gameObject); platformList.RemoveAt(0); } } 50. }
Your code is unreadable, and since you clicked on the wrong button, I cannot fix it for you. You need to:
Edit your question by clicking on 'edit'
Delete your existing code in the question
Past a new copy of your code into the question
Select your code
Click on the 101/010 button to format your code.
Answer by reefwirrax · Jan 01, 2014 at 11:06 AM
you get random numbers from Random.Range and PerlinNoise in Unity.
if you make the random number a function of position/pixels, you do (perlin noise((x+y float, seed)*500 )+x) %1= different float between 0 and 1 for every 2d point in space.
the plus x at the end stops perlin from having a bell curve and makes it linear.
Your answer
Follow this Question
Related Questions
Want 3 different Random Platforms in x direction 1 Answer
2D side scroller/flappy bird clone 0 Answers
How do you hang on to a ledge in a 2d game 1 Answer
How do I attach a script to a state 1 Answer