Instantiating object at wrong location, Unity bug?
Hi, I am having issues with bullets / muzzle flashes instantiating in the wrong spot / rotation on my AI characters. I've been trying to figure out the issue for a couple of days now but there doesn't seem to be any logical reason. The code works fine when my enemy is in one state (normal combat), he can walk around and crouch and the bullets and muzzle flashes will all spawn where they are supposed to. When he transitions to another state (cover combat) the bullets and muzzle flashes will usually spawn fine the first set of shots but then as soon as he crouches and stands back up they all spawn off to the side and on an angle.
Both the muzzle flash and the bullets have an empty game object spawn point at the end of the enemies rifle, this doesn't change at all. The code I use to instantiate doesn't change between states. The only difference I can see is that the enemy crouches, it is as if the world position for the spawn locations doesn't update when the character transitions back to standing. However it works in the normal state so I am stumped. Is this a bug? I am using the following code:
private GameObject shootBulletSpawnPoint;
private Quaternion origBulletRot;
void Start()
{
shootBulletSpawnPoint = controller.bulletSpawnPoint; // controller is my enemy AI manager, a script where I store public variables
origBulletRot = shootBulletSpawnPoint.transform.localRotation;
}
private IEnumerator FireWeapon()
{
shootBulletSpawnPoint.transform.localRotation = WeaponAccuracy(); // this function only marginally modifies the x and z values for the local rotation.
PoolBoy.Instance.Spawn(controller.bullet, shootBulletSpawnPoint.transform.position, shootBulletSpawnPoint.transform.rotation); // this is my pool manager, the issue occurs even when using standard unity instantiate.
GameObject muzzleFlashClone = PoolBoy.Instance.Spawn(controller.muzzleFlash, controller.muzzleFlashSpawnPoint.transform.position, controller.muzzleFlashSpawnPoint.transform.rotation);
muzzleFlashClone.transform.parent = transform;
shootBulletSpawnPoint.transform.localRotation = origBulletRot;
yield return new WaitForSeconds(controller.fireRate);
}
}
here is a gif of the issue : https://gfycat.com/EquatorialEqualIberianlynx , red is the combat normal state, and black is the cover state. I have also attached a picture showing the set up and how the bullet spawn point is in the correct position yet the bullets are spawning way off to the left.
Your answer
Follow this Question
Related Questions
bullet destroy on collison 1 Answer
Advance AI ( League of Legends ) 0 Answers
Get Opposite of a Rotation Stored In a Quaternion 0 Answers
this Bullet shooting script error 0 Answers
Record and replay controller movement 0 Answers