How to access the Transform of a moving child object?

I have an FPS controller with an empty child called “BulletSpawn”, which is supposed to serve as the spawn points for instantiated projectiles. When I instantiate a projectile and set its position and rotation equal to “BulletSpawn”, I do not get the intended result. This is because the transform of “Bulletspawn” under the inspect does not update at all. It moves correctly in relation to the parent FPS controller, but its transform values do not change while moving across the map. I have tried a couple different solutions and have a couple more ideas but I believe there should be a simple and clean solution to this. Please let me know if you can help because I’m approaching a deadline! according to the documentation, transform.position is the global/absolute transform value, and yet this does not seem to be the case, since transform/position and transform.rotation values stay the same in the inspector while moving all over the map. Is this a bug or am I missing something?

hayk

transform.Position returns the world space location, but the unity inspector only ever displays in local space.

if you need to test to see if it is working, just Debug.Log the result of transform.Position

1 Like

i.e. transform.localPosition :slight_smile:
https://docs.unity3d.com/ScriptReference/Transform-localPosition.html

1 Like

Thank you PasserbyMC.

Update: it works now. Bullets fly out of the bullet origin, in the correct direction. When I set bulletOrigin to be a child of the Rifle, bullets always come out of the the tip of the rifle, as intended. BullerOrigin has a script attached, with a function called instantiateBullet(), which spawns bullets. if i call istantiateBullet() in the Update function of the same script, an endless stream of bullets fly out of the gun and they work just fine. However, I need the instantiateBullet() to only run at a particular frame of the gun firing animation, so I have an animation event which calls a function in a script attached to the animated object (rifle). This function runs at the correct time, as evident by the deb.log statement. I have it set up so that the funtion of the animation event calls the instantiateBullet() function from the bulletOrigin script. When I have it set up this way, the bullet just does not spawn at the exact location of the bulletSpawn object.

I am beyond confused… what is going on? the problem seems to be in calling the instantiatebullet() function from another script. If I call the function from the same script for debugging purposes, it works fine. however I need to call it from another script so that it fires only at a particular frame of an animation.

what is going on…

Update: apparently everything works fine as long as I dont call the function bulletSpawn() from the animation event. I just tried making another empy object with a new script, and called BulletSpan from this new script. It works!
So really the issue is the animation event. why its causing this issue, I do not know. But I really need to use the naimation event to call the bulletspawn() script. Otherwise, the animation will not be synchronized with the shooting. when the mouse button is held down, the animation automatically loops and the bullet should come form a particular frame. i could just use mouse input to call bulletSpwan but that would not match with the animation very well.

Is this a bug with the animation event? is my bullerOrigin empty Gameobject somehow inheriting transform properties from the animated gun? how can i stop this?

SOLVED: I don’t quite understand why/how, but checking “apply root motion” in the animator component of the rifle, then selecting “animate physics” under “update mode”, then UN-checking “apply root motion” did the trick. Everything works fine now. I feel like I got lucky that I got it working… because I still don’t understand how this works. Unity is so confusing sometimes. I can’t tell if its buggy or if I just lack understanding of tools/concepts

thanks guys!