#Collision Issues
1 messages · Page 1 of 1 (latest)
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);
}
}
}
Add that log and see if you get anything
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?
How are you moving the object?
public class Bullet : MonoBehaviour
{
public float speed;
void Update()
{
transform.Translate(Vector2.right * speed * Time.deltaTime, Space.Self);
}
}```
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
Both objects actually, disable the movement of both
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.
All layers should collide by default, so unless you tinkered with that, that shouldn't be an issue
I'm using a Unity Asset called "Infinite Runner Engine" because I'm being forced to for this class. Still trying to get familiar with it.
That shouldn't matter much I don't think
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
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 ^
Just to make sure, your game is actually in 2D, right?
Yes
Could you make a video of the collision?
Yes, give me a few to make it
Bullets pass right through the zombie and nothing comes up in the console
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
Yeah, lemme try that rq.
Bullet is detecting that its hitting the ground, but not the zombie
Can you screenshot the bounding box of the zombie?
Yeah. Also if I change isTrigger on zombie to false, it works
What now I'm confused
Oh yeah that's fine
Oh yeah no that makes sense actually
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
The back and forth-ing of inspectors and components changing confused me which brought me to the wrong conclusion heh
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
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
e.g
public static class Tag
{
public const string Bullet = nameof(Bullet);
}
public class KillZombie : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("working");
if (collision.gameObject.CompareTag(Tag.Bullet))
{
Debug.Log("working");
Destroy(gameObject);
}
}
}
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)
That would be useful. Most times that only messes me up if I start the tag out with a capital letter and forget