#Collision Issues

1 messages · Page 1 of 1 (latest)

bronze remnant
#

Let's continue in this thread @gritty carbon

#
public class KillZombie : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collider)
    {
        Debug.Log("working");
        if (collider.gameObject.CompareTag("Bullet"))
        {
            Debug.Log("working");
           // Destroy(gameObject);
        }
    }
}
bronze remnant
gritty carbon
#

Nothing. It would have detected hitting the ground. It couldn't be an issue with the rb or the object moving too fast, could it?

bronze remnant
gritty carbon
#
public class Bullet : MonoBehaviour
{
    public float speed;
    void Update()
    {
        transform.Translate(Vector2.right * speed * Time.deltaTime, Space.Self);
    }
}```
bronze remnant
#

Yeah I don't think you should use transform because that fucks with physics

#

Could you just spawn in a bullet with 0 velocity for me?

#

And just drag the two objects over eachother

bronze remnant
gritty carbon
#

The zombie doesn't move. At least not yet. Just the bullet. Will test out dragging the bullet over the zombie now.

#

Yeah still nothing. I thought maybe it was the Physics 2D layer matrix or whatever its called, but I didn't see any issues with that.

bronze remnant
gritty carbon
bronze remnant
#

Could you share your entire inspector of both objects again? And the code? And maybe the hierarchy while you're at it heh

#

Sorry for asking so many times but it's kinda the only way to figure out what is wrong

#

In the meanwhile I'll brb

gritty carbon
#

There's a lot in the inspector for the zombie:

#

Bullet inspector:

#

Hierarchy after hitting play:

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

public class KillZombie : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collider)
    {
        Debug.Log("working");
        if (collider.gameObject.CompareTag("Bullet"))
        {
            Debug.Log("working");
           // Destroy(gameObject);
        }
    }
}```
#

code attached to zombie ^

bronze remnant
#

Just to make sure, your game is actually in 2D, right?

gritty carbon
#

Yes

bronze remnant
gritty carbon
#

Yes, give me a few to make it

#

Bullets pass right through the zombie and nothing comes up in the console

bronze remnant
#

Don't you just hit the ground?

#

Can you do the same but just slap the player in space not moving and a zombie to shoot at

gritty carbon
#

Bullet is detecting that its hitting the ground, but not the zombie

bronze remnant
gritty carbon
#

Yeah. Also if I change isTrigger on zombie to false, it works

bronze remnant
#

What now I'm confused

gritty carbon
bronze remnant
#

Oh yeah that's fine

bronze remnant
#

I got confused

#

Since the zombie is a trigger so it'd have to be OnTrigger

#

So that's my bad

#

But it works now

bronze remnant
gritty carbon
#

Just fixed it! Thank you so much for the help.

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

public class KillZombie : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        Debug.Log("working");
        if (collision.gameObject.CompareTag("Bullet"))
        {
            Debug.Log("working");
            Destroy(gameObject);
        }
    }
}```
#

Changing my code to this fixed it. I could have sworn I'd changed it to this before but I guess I messed up somewhere

bronze remnant
#

Lovely

#

Also, it might be worthwhile to have a class with constants of the tags

#

So you don't have to type them in as strings

#

Because if there's a typo stuff won't work

bronze remnant
#

But that's up to you, it's not necessary, but it might prevent errors

#

(Obviously there are more ways to do stuff like this, this is just an example)

gritty carbon
#

That would be useful. Most times that only messes me up if I start the tag out with a capital letter and forget