Tuesday, November 1, 2022
HomeGame Developmentunity - Rating counter not rising after recalling object from a pool...

unity – Rating counter not rising after recalling object from a pool with raycast detection hit


Good day, its me once more. I’ve been taking part in round with “Object Pool” and it’s fascinating the quantity of CPU efficiency can save up. Nevertheless, I’ve encountered a brand new “function” the place it scores as soon as for the article being referred to as and going by means of the pipe.

In my object pool, I’ve a pipe GameObject the place is being Instantiated and set its methodology Lively(false) till it’s spawned. As soon as spawned, my prefabs will likely be stuffed accordingly to the Object Pool’s dimension.

As soon as it spawns, it does what it ought to do, each scoring and the identical mechanic as “Flappy Chook”. Nevertheless, as soon as the article will get Lively once more, it would not appear to attain anymore as a result of it was handed by a participant. What I’ve performed is to have a flag that checks if the participant (chook) has handed the pipe or not. Nevertheless, after I go by means of it, it should replace it as if it was 6 instances (one level per body). You could ask “have you ever performed one other flag for the pipe itself?” then the reply is sure. I attempted that method additionally, however it should solely hold the rating as soon as and never enhance additional than 5.

I ran out of concepts with the article pool strategy. It appears to work advantageous whether it is WITHOUT object pooling, however the flaw right here is that it prices me CPU efficiency.

enter image description here

It both will increase it by simply 1, or it will increase it by 6 instances (as a result of for every body the article is within the ray, it counts one other level).

I’ve browsed on the Unity Studying heart to learn how to do the article pooling, and it wasn’t too unhealthy as I assumed. Later, I discovered this challenge and I assumed it was an issue with my logic (which it may be). I’ve spent a number of hours already (first mistake) to assume it’s one thing simple to repair, however apparently it wasn’t due the time I’ve spent to simply work out why it’s not working 😅. I’ve been fiddling round my RayCastDetection, SpawnManager, ObjectPooling, and PlayerControl logic that interacts accordingly to the way in which I need, however nada.

ObjectPooling.cs

public static ObjectPooling sharedInstance;
public Record<GameObject> pooledObjects;
public GameObject objectToPool;
personal PlayerControl playerControllerScript;
public int amountToPool;

void Awake()
{
    sharedInstance = this;
}
void Begin()
{
    playerControllerScript = GameObject.Discover("Participant").GetComponent<PlayerControl>();
    pooledObjects = new Record<GameObject>();
    GameObject tmp;

    for (int i = 0; i < amountToPool; i++) //Add objects to the pool and turns them invisible (false)
    {
        tmp = Instantiate(objectToPool);
        tmp.SetActive(false);
        playerControllerScript.passedBeam = false;
        pooledObjects.Add(tmp);
    }
}
public GameObject GetPooledObject()
{
    for (int i = 0; i < amountToPool; i++)
    {
        if (!pooledObjects[i].activeInHierarchy)
            return pooledObjects[i];
    }
    return null;
}

RayCastDetection.cs

public class RayCastDetection : MonoBehaviour
{
    public UIManager UIManagerScript;
    public PlayerControl playerControlScript;
    personal RaycastHit hit;
    public bool handed;

    void Begin()
    {
        UIManagerScript = GameObject.Discover("UI_Manager").GetComponent<UIManager>(); //Used for scoring
        playerControlScript = GameObject.Discover("Participant").GetComponent<PlayerControl>(); //used for participant passing by means of the pipe
        handed = false;
    }

    void Replace()
    {
        Vector3 beamDown = remodel.TransformDirection(Vector3.down);
        Ray ray = new Ray(remodel.place, beamDown);

        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.tag == "Participant" && handed == false)
            {
                handed = true;
                UIManagerScript.rating++; //Scoring occurs right here
                if (handed == true)
                    playerControlScript.passedBeam = true;
            }
            Debug.DrawRay(remodel.place, hit.level - remodel.place);
        }
    }
}

SpawnManager.cs

public class SpawnManager : MonoBehaviour
{
    public GameObject[] obstaclesPrefab;
    personal PlayerControl playerControllerScript;
    personal float startDelay = 1.69f;
    personal float repeatRate = 1.1f;

    void Begin()
    {
        playerControllerScript = GameObject.Discover("Participant").GetComponent<PlayerControl>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

    void Replace()
    {
        
    }
    void SpawnObstacle()
    {
        // int obstaclesIndex = Random.Vary(0, obstaclesPrefab.Size); //That is used provided that I do not wish to cope with object pooling, however the entire level is to make use of it. That is only a reference if I wish to return
        
        if (playerControllerScript.gameOver == false)
        {
            float randomY = Random.Vary(-2f, 2f);
            Vector3 randomHeight = new Vector3(35, randomY, -7);
            GameObject pipe = ObjectPooling.sharedInstance.GetPooledObject();
            if (pipe != null)
            {
                pipe.remodel.place = randomHeight;
                pipe.SetActive(true);
                //My guess is that I wish to instantiate the article pipe's beam to false right here 
            }
        }
            // Instantiate(obstaclesPrefab[obstaclesIndex], randomHeight, obstaclesPrefab[obstaclesIndex].remodel.rotation); //That is used provided that I do not wish to cope with object pooling, however the entire level is to make use of it. That is only a reference if I wish to return
    }
}

Be happy to go away some solutions in what I’ve missed out or any questions with reference to fill in. Thanks in your time!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments