1
\$\begingroup\$

I am trying to create doodle jump like game with object pool for platforms. I have created an empty object with box 2D collider attached to it. This object moves with player and should be gathering platforms which exit its collider, but it isn't.

enter image description here enter image description here enter image description here

public class PoolGrabber : MonoBehaviour {

[Tooltip("Field for game manager.")]
[SerializeField]
private GameObject gameManager;

private void Awake()
{
    gameManager = GameObject.FindGameObjectWithTag("GameManager");
}

private void OnTriggerExit2D(Collider2D collision)
{
    if(collision.tag == "PooledObject")
    {           
        collision.gameObject.SetActive(false);
        gameManager.GetComponent<LevelGenerator>().objectPool.Add(collision.gameObject);
    }
}

enter image description here

It also doesn't work with OnTriggerEnter2D.

I have also tried

private void OnBecameInvisible()
{
    gameObject.SetActive(false);
}

on a platform. Doesnt work either.


I have discovered that for all those methods above object required to has Rigidbody2D component. But what if I don't want any phisycs calculations to be handled. I don't need physics on those platforms at all. Also, an object doesn't become invisible if you see it in the editor window. Isn't very convenient.

\$\endgroup\$
1
\$\begingroup\$

You need a Rigidbody2D on one of the two colliding objects (moving objects with colliders always must have one). If you don't want the Rigidbody2D to be affected by forces but instead to be manually controlled you must set it to kinematic. The reasons to have a rigidbody on moving objects are two. The first one is that a moving collider without a rigidbody causes performance troubles for Unity. The second one is that if 2 objects are colliding and only the static one have rigidbody it is very likely it to be in "sleep" mode (for performance reasons) and so the collision will not be detected. There is an option on the rigidbody to disable the "sleep" mode.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.