Home Game Development unity – Score counter not growing after recalling object from a pool with raycast detection hit

unity – Score counter not growing after recalling object from a pool with raycast detection hit

0
unity – Score counter not growing after recalling object from a pool with raycast detection hit

[ad_1]

Hello, its me once more. I’ve been enjoying round with “Object Pool” and it’s fascinating the quantity of CPU efficiency can save up. However, I’ve encountered a brand new “function” the place it scores as soon as for the item being known as and going via the pipe.

In my object pool, I’ve a pipe SportObject the place is being Instantiated and set its technique Active(false) till it’s spawned. Once spawned, my prefabs can be crammed accordingly to the Object Pool’s dimension.

Once it spawns, it does what it ought to do, each scoring and the identical mechanic as “Flappy Bird”. However, as soon as the item will get Active once more, it does not appear to attain anymore as a result of it was handed by a participant. What I’ve accomplished is to have a flag that checks if the participant (chook) has handed the pipe or not. However, once I cross via it, it would replace it as if it was 6 occasions (one level per body). You could ask “have you ever accomplished one other flag for the pipe itself?” then the reply is sure. I attempted that means additionally, however it would solely preserve the rating as soon as and never improve additional than 5.

I ran out of concepts with the item pool method. It appears to work high-quality 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 occasions (as a result of for every body the item is within the ray, it counts one other level).

I’ve browsed on the Unity Learning middle to learn how to do the item pooling, and it wasn’t too dangerous as I assumed. Later, I discovered this concern 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 suppose it’s one thing straightforward to repair, however apparently it wasn’t due the time I’ve spent to only determine why it’s not working 😅. I’ve been fiddling round my RayCastDetection, SpawnManager, ObjectPooling, and PlayerManagement logic that interacts accordingly to the way in which I need, however nada.

ObjectPooling.cs

public static ObjectPooling sharedInstance;
public List<SportObject> pooledObjects;
public SportObject objectToPool;
non-public PlayerManagement playerControllerScript;
public int amountToPool;

void Awake()
{
    sharedInstance = this;
}
void Start()
{
    playerControllerScript = SportObject.Find("Player").GetComponent<PlayerManagement>();
    pooledObjects = new List<SportObject>();
    SportObject 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 SportObject GetPooledObject()
{
    for (int i = 0; i < amountToPool; i++)
    {
        if (!pooledObjects[i].energeticInHierarchy)
            return pooledObjects[i];
    }
    return null;
}

RayCastDetection.cs

public class RayCastDetection : MonoBehaviour
{
    public UIManager UIManagerScript;
    public PlayerManagement playerControlScript;
    non-public RaycastHit hit;
    public bool handed;

    void Start()
    {
        UIManagerScript = SportObject.Find("UI_Manager").GetComponent<UIManager>(); //Used for scoring
        playerControlScript = SportObject.Find("Player").GetComponent<PlayerManagement>(); //used for participant passing via the pipe
        handed = false;
    }

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

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

SpawnManager.cs

public class SpawnManager : MonoBehaviour
{
    public SportObject[] obstaclesPrefab;
    non-public PlayerManagement playerControllerScript;
    non-public float startDelay = 1.69f;
    non-public float repeatRate = 1.1f;

    void Start()
    {
        playerControllerScript = SportObject.Find("Player").GetComponent<PlayerManagement>();
        InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
    }

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

Feel free to go away some options in what I’ve missed out or any questions with regard to fill in. Thank you on your time!

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here