Whenever i try to instantiate object during animation it spawns at completely wrong position and rotation. I’ve never experienced this bizarre behavior…
My rig setup is a GunPivot transform (zeroed out) that is parented to hand bone transform and under that is weapon model + bullet spawn.
Heres the code i use. I use animation event from different script to call it during animation.
Bullet spawns as its supposed to if im using the test in Update(), which is straight out of the barrel.
BUT whenever i do it during shoot animation its as if the parented object didnt move with the hand even though it does in the editor window. I’ve attached picture visualizing this. Im not sure if its just coincidence but the wrong spawn position is very close to the position of barrel in idle position. I also checked that i didnt accidentally keyframe something in the gun but thats not the case.
(Link to the picture since images dont seem to work on this forum…)
public class GameObjectSpawner : MonoBehaviour
{
public GameObject prefab;
public bool test;
private void Update()
{
if (test)
{
test = false;
SpawnObject();
}
}
[Tooltip("Use this gameObject as parent?")]
public bool useParent = false;
public void SpawnObject()
{
if (!useParent)
{
GameObject go = Instantiate(prefab) as GameObject;
go.transform.position = transform.position;
go.transform.rotation = transform.rotation;
//go.transform.rotation = Quaternion.LookRotation(transform.forward);
Debug.Break();
}
else
{
GameObject go = Instantiate(prefab, this.transform);
}
}
}