How To Generate Platforms Down Each other in a pattern with some different positions Unity
i am instantiating a platform below one another which you can see in below pic
but i want to generate platforms like this
so how i can do this below is my code which generate platforms below each other only in Y position
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class platformGeneration : MonoBehaviour
{
public int numOfPlatforms;
public GameObject platform;
public Transform spawnPosition;
// Start is called before the first frame update
void Start()
{
Vector3 pos = spawnPosition.GetComponent<Transform>().position;
for (int i = 0; i < numOfPlatforms; i++)
{
spawnPlatofrm(pos + new Vector3(i+2,-i - 5, 0));
//spawnPlatofrm(pos + new Vector3(0, -i - 5, -i - 2));
//for (int j = 0; j < numOfPlatforms; j++)
//{
// spawnPlatofrm(pos + new Vector3(0, -j - 5, -j - 2));
//}
//spawnPlatofrm(pos + (Vector3.right * i));
}
}
void spawnPlatofrm(Vector3 spawnPosition)
{
Instantiate(platform, spawnPosition, Quaternion.identity);
}
}
Answer by tormentoarmagedoom · May 16, 2019 at 10:00 AM
Hello.
Are you asking to generate platoforms, 1 by 1, down the lastone, but one is a little right side, next is left side, right, left... etc.. is correct?
Then you only need to continue instantiating like now, but have something to know if this have to be at right or at left.
FIrst, you are doing adding some constant values to this series, i dont know if you know why are there.. i wil ldelete them to easy understand what I'm doing
void Start()
{
Vector3 pos = spawnPosition.GetComponent<Transform>().position;
for (int i = 0; i < numOfPlatforms; i++)
{
int k = 1;
if (k ==1)
{
k = -1;
}
else
{
k = 1;
}
spawnPlatofrm(pos + new Vector3(k,-i, 0));
}
}
If you see, y component (i) will be lower and lower every loop. And x compoenent (k) will be 1 and then -1 then 1 again, -1, etc...
Bye!
Your answer
Follow this Question
Related Questions
Hold touch at the screen to move object 0 Answers
Stuck With this codes. 1 Answer
Destroy instantiated Projectile after time 1 Answer