Moving a child object to match where the parent collided with the child OnCollisionEnter

Hello guys,

I have been breaking my head for a while and I decided to ask here after all. In my game the player shot an arrow to a fish and I want the arrow to make the fish it child but keep the arrow at the point where it hit the fish before it made it its child. I have look all over the web and found no solution so I am counting on someone here who can shine some light on this. How to do it.

Situation detailed:

void OnCollisionEnter(Collision collision) {if(collision.gameObject.CompareTag(“Headshot”))
{
fish = collision.transform.root.GetComponent();
fish.transform.parent = arrowTransform;//By now the arrow is at the origen of the gameObject instead of the point it hit it!
}

}

player shot the arrow. Arrow hit fish(say by the head) on collision enter I get the point where it hit the fish. Now based on the logic of the game the fish has to become the child of the arrow in order for me to manipulate the fish through the arrow. The problem, because this is happening fast by the time I parent the fish to the arrow the arrow is at the origin of the fish and not by the head.

So I thought, there has to be a way to move the fish after parented to the arrow so it matches the point where the fish was hit. but nothing work for me so far in this regard.

Any help is very much appreciate it. I code in C#.

Regards,
Carlos

work out the offset between the collision point and the fish’s position, then set that as the localPosition

fish.localPosition = collision.point - fish.transform.position; // might be the other way around

alternatively, you can use the setparent function instead of the direct assignment and use the optional second parameter to retain the local position/orientation to do pretty much the same

(this variant of the function is a fairly recent addition I think, so it might be version limited)

In a collision there may be multiple contacts, but usually there will only be one and it will be the point where the arrow is hitting the fish, so you can do this:

fish.transform.SetParent(arrowParent);
fish.transform.position = collision.contacts[0].point;

Thanks guys for your input regarding this issue I have. Unfortunately neither
fish.localPosition = collision.point - fish.transform.position(or the other way around) or SetParent(), fixed the situation.

Can anyone help please? I am really going crazy with this, pulling my hairs!

Thanks in advance