0
\$\begingroup\$

my bullet:

  1. There is a Box collider (checked "is Trigger")
  2. It has Rigidbody2D (Gravity = 0, freeze rotation Z)

https://imgur.com/T4j1QoO

I added the "OnTriggerEnter2D" function because isTrigger is checked. Bullet script:

private float lifeTime = 1.0f;

private void Start()
{
    Destroy(gameObject, lifeTime);
}

private void OnTriggerEnter2D(Collider2D collision)
{
    GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
    Destroy(effect, 0.2f);
    Destroy(gameObject);
}

When I fire, the OnTriggerEnter2D function works even though there is no collider/object in the direction of the bullet. It only works a certain distance away from my main character, and it's always the same no matter which direction I shoot.

\$\endgroup\$
2
  • \$\begingroup\$ You'll need to walk us through the complete steps to build a Minimal Complete Verifiable Example of this in a new empty project. Once we can reproduce the problem, we can test potential fixes. \$\endgroup\$
    – DMGregory
    Nov 8, 2021 at 20:04
  • 1
    \$\begingroup\$ You could set a breakpoint inside and cehck against the collision what exactly it collides against. Maybe your floor or an background image \$\endgroup\$
    – Zibelas
    Nov 8, 2021 at 20:15

1 Answer 1

0
\$\begingroup\$

Here's how I fixed the inside of my OnTriggerEnter2D function:

if (collision.tag == "Enemy")
    {
        GameObject effect = Instantiate(hitEffect, transform.position, Quaternion.identity);
        Destroy(effect, 0.2f);
        Destroy(gameObject);
    }

The problem is fixed, but why do I have to do an "if" check? Doesn't the trigger function already check this?

If it only touches things with tags "Enemy" and colliders... I guess I'll have to add this for many different objects?

EDIT: I have a pistol collider in the position where the bullet was created, I made the pistol collider a little smaller and the problem went away. However, I adjusted this so that the colliders of the pistol and the bullet do not intersect, and it looked like that on the scene screen. Also, I couldn't quite understand the problem since the projectile was destroyed at a much greater distance from my pistol.

\$\endgroup\$
6
  • \$\begingroup\$ Try using a Debug log to figure out what it's colliding with that is not an enemy. That should give you a good clue. \$\endgroup\$
    – DMGregory
    Nov 8, 2021 at 22:01
  • \$\begingroup\$ I tried without the if block. The function worked even though there was no object in the scene (I put debug.log inside the function) \$\endgroup\$
    – gunza
    Nov 8, 2021 at 22:08
  • \$\begingroup\$ What do you get from Debug.Log($"Collided with {collision.gameObject.name}") in that situation? \$\endgroup\$
    – DMGregory
    Nov 8, 2021 at 22:13
  • \$\begingroup\$ It shows my "Pistol" object that I fired. But this is impossible because I don't have a pistol in the positions where the projectile is destroyed. While shooting, I check it from the scene screen and the collider of my pistol and the collider of the bullet do not intersect. \$\endgroup\$
    – gunza
    Nov 8, 2021 at 22:42
  • \$\begingroup\$ Sounds like you should edit your question then to walk us through the steps to recreate this problem. If you say it's impossible we can only take your word for it, which isn't going to lead us to a solution. But if you can show us how to set up a Minimal Complete Verifiable Example, then we can investigate and find out why it's possible after all. \$\endgroup\$
    – DMGregory
    Nov 8, 2021 at 22:44

You must log in to answer this question.

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