C# Need Help Refining Ricochet Script

I’ve added a ricochet function to my script. But the script doesn’t ricochet like I was expecting it to. My logic with the ricochet is that when the 2d projectile collides with a gameobject it rotates along the z axis minus the other gameobject’s transform.rotation.z to have it look like it ricocheted off the gameobject. However for some reason the projectile always rotates upward no matter what other’s transform.rotation.z is. Is there a flaw with the logic to my script?

public float speed;
public bool ricochet;

    	void Update () 
    	     rigidbody2D.velocity = transform.up * speed; 
    void OnTriggerEnter2D(Collider2D other)
    		if(ricochet == true){
    		   transform.rotation = Quaternion.Euler(0,0,transform.rotation.z - other.transform.rotation.z);

Hi, I’ve recently made a script that controls a bullet that can ricochet. I used the Vector2.Reflect function to get a reflected vector and then Mathf.Atan2 to get the Z-rotation.

private void ReflectFrom(ContactPoint2D contactPoint) {
  var reflected = Vector2.Reflect(_transform.right, contactPoint.normal);
  var rotationZ = 90f - Mathf.Atan2(reflected.x, reflected.y) * Mathf.Rad2Deg;
  _transform.eulerAngles = new Vector3(0f, 0f, rotationZ);

contactPoint is the first contact point I get from the Collider2D object that gets passed to OnCollisionEnter2D.

As to why your object always moves up: I think your rigidbody2D probably isn’t rotated with transform. Maybe try something like this:

rigidbody2D.rotation = Quaternion.Euler(0, 0, transform.rotation.z - other.transform.rotation.z);

You also might want to move your rigidbody moving to the FixedUpdate method. From what I understand, the best practice is to calculate the movement in Update and move within FixedUpdate to keep physics as accurate as possible.