Rotate gameobject, keep child in fixed position?

I have a script that launches a bullet in any direction, this sets the bullet gameobject to face the current direction. However, since I’m making a top down 2d game, i need to have a “wallCollider” 1 unit below in Y-axis, and it needs to be a child of the bullet gameobject.

It works fine if I fire right (see image 1), but when the gameobject gets rotated the wallCollider as a child of course rotates along with it (image 2).

How can I find the local position for the child, after it’s been rotated? (Image 3)
I guess the localRotation should be 45 if the bullet is rotated -45 like the image below.

I’ve tried something like this, but it doesn’t work at all and sets the wall colider to 46,0.3,0:

var worldPosition = transform.InverseTransformPoint(new Vector3(0,-1,0));
wallCollider.localPosition = wallCollider.TransformPoint(worldPosition);

How do I solve this? Thank you in advance! :slight_smile:

What prevents you from resetting the global rotation of the object containing the wall collider? Simply set its (global) transform.rotation to 0,0,0 (or whatever direction you always must have) and you should be done.

Aha, I haven’t worked enough with global and local, but I guess it makes sense, I’ll try that :slight_smile:

I’d probably just unparent the wall collider from the bullet, rotate the bullet, then make it a child again and see how that works/performs. Only if that didn’t work or had poor performance would I start messing with rotating the wall collider after rotating the bullet.

Thanks, thats also a great suggestion!