sakibguy / worked-issues

arget medi a[rpescriptionmedicie] &ssets[dr] (reeeverse._ax:p)
2 stars 0 forks source link

[ORG] RU-MAKEAPP: Rendered on scene not in device screen #26

Closed sakibguy closed 1 year ago

sakibguy commented 2 years ago

Problem

20220508_4 1

sakibguy commented 2 years ago

GroundGenerator.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GroundGenerator : MonoBehaviour
{
    public Transform groundPoint;
    // public Camera fpsCam;
    public ObjectPooler[] groundPoolers;
    private float[] groundWidths;

    // Start is called before the first frame update
    void Start()
    {
        groundWidths = new float[groundPoolers.Length];
        for(int i = 0; i < groundPoolers.Length; i++) 
        {
            groundWidths[i] = groundPoolers[i].pooledObject.GetComponent<BoxCollider2D>().size.x;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x < groundPoint.position.x)
        {
            int random = Random.Range(0, groundPoolers.Length);
            float distance = groundWidths[random]/2;

            transform.position = new Vector3 (
                transform.position.x + distance,
                transform.position.y,
                transform.position.z
            );

            GameObject ground = groundPoolers[random].GetPooledGameObject();
            ground.transform.position = transform.position;
            ground.SetActive(true);

            transform.position = new Vector3 (
                transform.position.x + distance, 
                transform.position.y,
                transform.position.z
            );
        }
    }
}
sakibguy commented 2 years ago

ObjectPooler.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPooler : MonoBehaviour
{
    public GameObject pooledObject;
    public int numberOfObject;
    private List<GameObject> gameObjects;

    // Start is called before the first frame update
    void Start()
    {
        gameObjects = new List<GameObject>();
        for(int i = 0; i < numberOfObject; i++)
        {
            GameObject gobj = Instantiate(pooledObject);
            gobj.SetActive(false);
            gameObjects.Add(gobj);
        }
    }

    public GameObject GetPooledGameObject()
    {
        foreach(GameObject gameObject in gameObjects)
        {
            if(!gameObject.activeInHierarchy) 
            {
                return gameObject;
            }
        }

        GameObject gameObj = Instantiate(pooledObject);
        gameObj.SetActive(false);
        gameObjects.Add(gameObj);

        return gameObj;
    }
}
sakibguy commented 1 year ago

Fix when start cs unity foundation coding again.