transfer velocity from bullet to wall

when hitting an object with a bullet, how do i transfer the velocity from the bullet to the object, then destroy it? I know how to transfer velocity, just I don’t know how to do it with a bullet object. Here’s my code:

public class bullet : MonoBehaviour
{
    public float speed = 20f;
    public Rigidbody2D rb;
    private Rigidbody2D velocityObject;
    void CopyVelocity(Rigidbody2D from, Rigidbody2D to)
    {
        Vector3 vFrom = from.velocity;
        Vector3 vTo = to.velocity;

        // Move the values you want for each exis
        vTo.x = vFrom.x;
        // vTo.y = vFrom.y; // Leaving y-axis as is
        vTo.z = vFrom.z;

        to.velocity = vTo;
    }

    void Start()
    {
        rb.velocity = transform.right * speed;
    }
    void OnTriggerStay2D(Collider2D hitInfo)
    {
        velocityObject = ;
        CopyVelocity(rb, velocityObject);
        Destroy(gameObject);
    }
}

(most of my code is just a bunch of tutorials thrown together like a nicely blended smoothie)

Hi @harrison4354,

You can get the object that the bullet collides with by using the hitInfo parameter and then with the property gameObject as the following.

void OnTriggerStay2D(Collider2D hitInfo)
{
         velocityObject = hitInfo.gameObject.GetComponent<Rigidbody2D>();
         CopyVelocity(rb, velocityObject);
         Destroy(gameObject);
}

Would it be possible for you to try this and then verify if this is what you’re trying to achieve?