Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problems instantiating platforms and overlaping each other C#

Discussion in '2D' started by UnityMai, Jul 22, 2016.

  1. UnityMai

    UnityMai

    Joined:
    Jan 4, 2016
    Posts:
    5
    Hello everyone in Unity community! I am very happy with the program Unity and I am already using it and learning as i go. Unfortunately everytime I come to a problem I try really hard to fix it on my own and search for answers on net or forum. This time I am unable to fix it on my own and I am asking for assistance. I am still quite new at Unity and wish to learn it better.

    So to get to my problem. I am building a 2d infinite jumper platformer and I am having big problems instantiating the platforms the way I want. The if statements arent working as they should and they also spawn platforms on top of each other. I want to make the spawning one after another and not spawn on top of one another. And how do you only pause spawning a platform at a specific height to spawn something else (like coins) and then spawn platforms again?

    The regular platforms should spawn most frequently while Exjump should spawn only sometimes and not overlap regular platforms nor Coins3. And the Coins3 (not platform) should spawn only after eighter ExJump or after Regular platform. I even tried creating a prefab where i place a platform + coins, not sure if i could work with that as well?

    Thank you for all help in advance!.

    Here is a code im working with:
    -------------------------------------------------------------------------------------------------------------------------------------------

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;

    [RequireComponent(typeof(ManagerPickup))]
    public class GameManager2D : MonoBehaviour
    {
    float playerHeightY; // height at which camera will adjust to

    public Transform player; // object to track is player
    public Transform regular; // store platforms from
    public Transform ExJump;
    public Transform LeftRight;
    public Transform UpDown;
    public Transform Coin3;

    private int platNumber; // used to assign specific prefabs to a number 1, 2,3 or 4
    private float platCheck; // checks if player height is near and spawns platforms
    private float spawnPlatformsTo; // previous locations of platforms were spawned from

    void Start ()
    {
    // finds our player gameobject using Player tag
    player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    // Update is called once per frame
    void Update ()
    {
    // Tracks our current playerHeight y axis
    playerHeightY = player.position.y;

    // if playerheight y is greater then 0 then perform platformmanager method
    if (playerHeightY > platCheck)
    {
    //calls platformmanager method
    PlatformManager ();
    }
    // Tracks our current camera y axis
    float currentCameraHeight = transform.position.y;
    // How our newheight of camera will adjust
    float newHeightOfCamera = Mathf.Lerp (currentCameraHeight, playerHeightY, Time.deltaTime * 10);
    // adjustment moves camera up if player height is higher then current camera height

    if (playerHeightY > currentCameraHeight)
    {
    // new camera position = its current x, new height for camera, current z
    transform.position = new Vector3 (transform.position.x, newHeightOfCamera, transform.position.z);
    }
    }
    void PlatformManager()
    {
    platCheck = player.position.y + 20;
    PlatformSpawner (platCheck + 20);
    }
    void PlatformSpawner(float floatValue) // spawnpoint is a float value that can be given
    {
    // y starts at 0, spawn platform starts at 0, this is used as a loop
    float y = spawnPlatformsTo;
    // while y (at start y = 0 -= 32, then will run until y > 32
    while (y <= floatValue)
    {
    // assigns a number -3.25 and 3.25
    float x = Random.Range (-2.33f, 2.33f);
    // refference a number for a platform between 1-4
    platNumber = Random.Range(1,4);
    // combine x and y values to create a vector 2
    Vector2 posXY= new Vector2 (x,y);
    // use the platnuber randomly picked to spawn a specific platform

    if (platNumber == 1)
    {
    // create a platform regular at our created vector 2 posXY and its rotation of 0
    Instantiate (regular, posXY, Quaternion.identity);
    //Instantiate(Coin2,posXY,Quaternion.identity);
    y += Random.Range (.5f, 4.5f);
    Debug.Log ("Spawned Regular"); // shows how many platforms are spawned anytime while y < 32 at start
    // reasigns spawnplatformsto to our float value, platforms are not spawned bellow an already spawned area
    spawnPlatformsTo = floatValue;
    }
    else if (platNumber == 2)
    {
    Instantiate (Coin3, posXY, Quaternion.identity);
    y += Random.Range (20.5f, 20.5f);
    Debug.Log ("Spawned Pickup");
    spawnPlatformsTo = floatValue;
    }
    else if (platNumber == 3)
    {
    Instantiate (ExJump, posXY, Quaternion.identity);
    y += Random.Range (10.5f, 15.5f);
    Debug.Log ("Spawned Pickup");
    spawnPlatformsTo = floatValue;
    }
    else
    {
    return;
    }
    }
    }
    }
     
  2. UnityMai

    UnityMai

    Joined:
    Jan 4, 2016
    Posts:
    5
    Please I would like someone to look at my code and help me solve my issues. I been working on everything else on my own and this is the first thing im asking for assistance. If i asked wrong questions please let me know.

    Thank you
     
  3. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
    use coroutines for spawn objects in random time:

    Code (CSharp):
    1. StartCoroutine(Spawning());
    2. IEnumerator Spawning()
    3. {
    4. yield return new WaitForSeconds(1.0f);
    5. while(isActiveAndEnabled)
    6. {
    7.    if (platNumber == 1)
    8.       ...
    9.    if (platNumber == 2)
    10.       ...
    11.   // waiting random time
    12. yield return new WaitForSeconds(Random.Range (10.5f, 15.5f));
    13. }
    14. }
     
  4. UnityMai

    UnityMai

    Joined:
    Jan 4, 2016
    Posts:
    5
    Thank you for response. Where exactly do I put the StartCoroutine(Spawning()); ? INumerator im familiar it goes at the end of the code outside of voids and then i have to call it somewhere so it goes active. Im not sure where to put it exactly. Do i have to make a new void or is it part of Void PlatformSpawner?
     
  5. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    194
  6. UnityMai

    UnityMai

    Joined:
    Jan 4, 2016
    Posts:
    5
    Just did that and also tried a couple of testing. I tried changing the PlatformSpawner, removing it, disabling posXY and changing Instantiate (ExJump, posY, Quaternion.identity) ect. Errors keep poping out alot of errors of posXY and not finding Y, cannot convert to vector3, Quaternion has some invalid arguments. So im not sure if i still can use the Platformanager or player height so its still combined for PlatformSpawner.
     
  7. UnityMai

    UnityMai

    Joined:
    Jan 4, 2016
    Posts:
    5
    Thank you I guess I will spend some more time on this code and see what i can do with it.

    Still no specific progress, but im gonna try more with Coroutines, add another similiar script like this one to separate platforms from coins (originally already did that). My original script works ok on first if statement, but after that not so much. If you would to disable the other if statements or try platNumber (1,2) the platforms spawn in a nice algorithm, but not with the other if statements included (they mess the 1st one up). I will also try the min/max height where they should be spawned.

    I am still searching for other solutions, but not sure how that will go. Coroutine didnt work properly and platforms/coins still overlapped each other when spawning.
     
    Last edited: Jul 23, 2016