0
\$\begingroup\$

I've seen questions like this that usually end up with the OP having confused localposition and world position. This is not the case.

My enemy is animated. He holds a gun. This gun has a child, called "shootpoint" which is where the bullet comes from.

When firing, I instantiate a empty gameobject at this shootpoint. Using the shootpoint's transform.position. The enemy is stood on the ground, so the shootpoint position is pretty low.

Here is the code that I am using to instantiate this.

GameObject g = new GameObject();
g.transform.position = bulletSpawnPoint.position;

This object (and the weapon's bullets) appear very high up. Far away from the position. Attached is a image, showing the point that the bullets (and the gameobject) are coming from. Selected, is where they should be coming from. They are instantiated in world space, using its transform.position. Here is a image.

Unity is bugged. I have no idea what to do now. How would I even fix this? Could it be the animator? Why doesn't it line up?

\$\endgroup\$
7
  • \$\begingroup\$ One thing to mention. The positions line up if I change the gun's parent. \$\endgroup\$
    – LeytonMate
    Oct 19, 2022 at 16:37
  • \$\begingroup\$ We'll need you to walk us through the steps to produce a minimal complete verifiable example of this problem. I suspect this is not an engine bug, but a result of some details in how you've authored your content. So we'll need a full walkthrough of the steps to create content that exhibits this problem. Problems that occur depending on parenting are often related to non-uniform scale somewhere in the transformation hierarchy. Interactions between colliders can also be a factor. So be sure your MCVE covers those aspects. \$\endgroup\$
    – DMGregory
    Oct 19, 2022 at 17:24
  • \$\begingroup\$ @DMGregory I'm not even really sure what has caused it. I'm also suspecting that, but all the real factors are that the enemy is animated, and the gun is a child of a bone on the enemy. When I say this is literally just a gun parented to the hand bone, I mean it as blatantly as possible. I'm convinced it's maybe something to do with it having a lot of parents. I don't know. \$\endgroup\$
    – LeytonMate
    Oct 19, 2022 at 20:29
  • \$\begingroup\$ I'm not asking you to guess what caused it, I'm asking you to determine the minimal steps needed to reproduce this. You can do this by creating a new, empty project, and recreating the problematic setup step by step, recording what you're doing at each stage. Or by copying your existing project, and deleting as much as you can while keeping the problem visible. Either way, you end up with a scene with as little content as possible, and you can give us instructions for how to build that exact scene that reproduced the problem. \$\endgroup\$
    – DMGregory
    Oct 19, 2022 at 20:34
  • 1
    \$\begingroup\$ I really can't tell what goes on in that image you added. The yellow stuff seems to be some level geometry, but that's where my understanding stops. What's the marked object? What are those exclamation marks around it? What's that fan of magenta-colored lines in the upper left? You said you figured out the problem yourself, so I guess the point is moot. But the next time you might want to add a bit more description to an image to explain what actually goes on in it. \$\endgroup\$
    – Philipp
    Oct 20, 2022 at 11:07

1 Answer 1

1
\$\begingroup\$

Turns out, I fell for the classic "the object referenced is actually the prefab, and not the actual instantiated object" gag.

I did not actually state "hey, the current weapon is the actual instantiated one, not the prefab" after instantiating the weapon.

\$\endgroup\$
1
  • \$\begingroup\$ Glad you found it! I hope the process of distilling an MCVE helped narrow it down. \$\endgroup\$
    – DMGregory
    Oct 20, 2022 at 11:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .