Home > Net >  Can I instantiate prefabs from an animation?
Can I instantiate prefabs from an animation?

Time:08-18

I have this animation where my character loads up their gun then shoots. I wish there was a way of instantiating the bullet in the animation, instead of having to do that outside of the animation (e.g. in code). I can get around this using one of the following:

  1. Add a bullet spawner to the player, which would expose a boolean flag "isBulletRequested". In the animation I can turn this flag on towards the end, and then the bullet spawner knows how to spawn the bullet and reset the flag.
  2. Similar to the above, but more generically--add a "prefab spawner" to the player, which has an exposed filed "spawnCommand". It would have values like "spawnCommand=Bullet.prefab (0, -1, 1)", which is asking to instantiate the bullet prefab at the Vector3 position provided.

Even though the solutions above work fine, I don't like them because it's not a smooth workflow; I'll need to create the animation in blender, then have to modify it in Unity to add the spawn. And every time I modify the animation I have to repopulate the spawn commands.

Any thoughts on how to handle this better?

CodePudding user response:

You can used Animation Events Just create MonoBehaviour, where save Pivot Transform for spawn bullet position(empty GameObject join to Gun). And create Bullet when Animation Event triggered. Bullet position will be equals PivotTransform.position.

  • Related