0
\$\begingroup\$

So, I'm quite new to game development and am trying to make a clone of flappy bird. The way my game operates is that I am spawning the pipes to the right of the camera, and then making it move all the way left until the camera cannot see it again, and place a square there that will collide with the pipes, destroy it and spawn it again to the right of the camera. Problem is, although the pipes can sense collisions, somehow when I use CompareTag(tag of the square) the collisions will not register at all. A debug message would not even load. They are both on the same layer so that can be eliminated. Here is the relevant code:

public class PipeSpawner : MonoBehaviour
{
    // Start is called before the first frame update
    float speed;
    float ypos;
    float spawnPoint;

    float delay;
    void Start()
    {
        speed = 1.5f;
        spawnPoint = -0.03f;
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.position -= new Vector3(speed,0,0) * Time.deltaTime;
    

        
    }

    void SpawnPipe() {
        ypos = Random.Range(-0.5f, 0.9f);
        Instantiate(this.gameObject,new Vector3(spawnPoint,ypos,transform.position.z),Quaternion.identity);

    }


    void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.CompareTag("Destroy")) {
            Debug.Log("hit");
            
        }

    }


and here are the pics of the inspector

The Pipes:

https://i.sstatic.net/8e6SA.jpg, https://i.sstatic.net/WXk1q.jpg;

The Square:

https://i.sstatic.net/hJAOz.jpg

If anything about my question is confusing, please tell me and I will try to make it clearer to the best of my ability. Thanks in advance.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D object, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherCollider)
{
    if (otherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject); 
        SpawnPipe();         
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Your solution worked. Thanks! \$\endgroup\$
    – Kevin H
    Commented Nov 9, 2021 at 13:59
  • \$\begingroup\$ @KevinH When this answer answered your question to your satisfaction, please accept it by clicking on the checkmark-icon next to it. \$\endgroup\$
    – Philipp
    Commented Nov 9, 2021 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .