Destroying a bullet.

Hello, I’m having a problem. I have bullets which are destroyed when they collision with a wall or something. This works fine but it doesn’t look well, you can see how the bullet goes through the wall for a second.
I have tried to change the collision detection to continuous, but it doesn’t make any difference.

Hola, estoy teniendo un problema. Tengo balas que son destruidas al momento de colisionar con una pared o cualquier otra cosa. Esto funciona bien, sin embargo, el efecto no queda bien. Se llega a ver como la bala traspasa la pared por un momento y después se destruye.
Ya intente cambiar la detección de colisiones a continua, pero no cambia nada.

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

public class BulletMovement : MonoBehaviour
{
    public float speed;

    
    void Start()
    {
        
    }

 
    void Update()
    {
        transform.Translate(speed*Time.deltaTime, 0, 0);
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (!collision.gameObject.CompareTag("Player"))
        {
           
            Destroy(gameObject);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.gameObject.CompareTag("Player"))
        {
            Destroy(gameObject);
        }
    }
}

You’re moving the “bullet” by effectively teleporting it every frame. By using Transform.Translate() to move it, it’s not working based on the physics system.

Why does this matter?

The physics system is handled with separate timing to the general rendering. If you were moving the bullet using physics, it would detect the collision/trigger immediately and the bullet wouldn’t have a chance to be rendered inside the object it’s colliding with.

Instead, you’re moving the object during the rendering frames, then the physics system eventually checks it, realizes that it’s moved and is now inside of something, then sends the message via OnCollisionEnter()/OnTriggerEnter().

The most direct conversion would be to use Rigidbody2D.MovePosition(), though a generally-more appropriate solution would be to use something more like:

Rigidbody2D rb;
// ...
void Start()
{
	rb = gameObject.GetComponent<Rigidbody2D>();
	// multiply by mass if a guaranteed-fixed speed is desired
	rb.AddForce(new Vector2(speed, 0f), ForceMode2D.Impulse);
}