Home Unity Instantiate Game Mechanic only works half of the time C#
Reply: 0

Unity Instantiate Game Mechanic only works half of the time C#

user2166
1#
user2166 Published in May 28, 2018, 5:04 am

I am trying to make a small game for iPhone a simple game mechanic where some platforms will spawn at either 90 degrees, -90 degrees, or 0 degrees in y rotation.

It is supposed to spawn in line with no spaces or gaps between the platforms.

It works about half the time. Half of them have spaces and the others don't.

Here is my script. You don't need to retype the whole thing. Sorry if it is sloppy.

public class General : MonoBehaviour
{

    public int time;
    public int timeLev;
    public int speed = 7;
    public GameObject platformTypeOne;
    public float platPosx;
    public int ran;
    public float platLegnth; 

    public int[] platRotationSel = new int[] { 0, 90, -90};
    public int platRotation;

    void Start()
    {
        StartCoroutine("PlatSpawn");

    }

    void Update()
    {

        time = (int)(Time.time % 1000000f);

        speed = time / 3;
        platRotation = platRotationSel[Random.Range(0, 3)];
        Debug.Log(platRotation.ToString());

    }

    IEnumerator PlatSpawn()
    {
        while (true)
        {
            if (platRotation == 90 || platRotation == -90)
            {
                platLegnth = 2.5f;
            }
            else
            {
                platLegnth = 5f;
            }
            ran = Random.Range(0, 4);
            platPosx = platPosx + platformTypeOne.transform.position.x + platLegnth;
            Instantiate(platformTypeOne, new Vector3(platPosx, 0, 0), transform.rotation);
            yield return new WaitForSeconds(0.5f);
            if (platRotation == 90 || platRotation == -90)
            {
                platLegnth = 2.5f;
            }
            else
            {
                platLegnth = 5f;
            }
            ran = Random.Range(0, 4);
            platPosx = platPosx + platformTypeOne.transform.position.x + platLegnth;
            Instantiate(platformTypeOne, new Vector3(platPosx, 0, 0), Quaternion.Euler(new Vector3(0, platRotation, 0)));
            yield return new WaitForSeconds(0.5f);
        }


    }
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.244487 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO