I have weird glitch in my game. When player shoot the gun, bullet is instantiated something called "ShootPoint" transform.
This is the ShootPoint's position:
And this is the code of firing bullet:
RigidbodyProjectile CreateBullet() {
GameObject bullet = Instantiate(bulletPrefab, shootPoint.position, shootPoint.rotation);
RigidbodyProjectile projectile = bullet.GetComponent<RigidbodyProjectile>();
projectile.rigidbody.AddForce(shootPoint.forward.normalized * bulletSpeed, ForceMode.VelocityChange);
return projectile;
}
Simple script to just instantiate bullet prefab in ShootPoint.transform.
However when I run the game, bullet generated totally wrong place:
That picture captured right after creation, even before push the bullet. Here's the updated code for capture that moment, I intentionally throw the error so that makes game stop:
RigidbodyProjectile CreateBullet() {
GameObject bullet = Instantiate(PrefabManager.GetPrefab("bullet"), shootPoint.position, shootPoint.rotation);
// Throw the exception
throw new System.Exception("DIE");
// So codes under here will not work
RigidbodyProjectile projectile = bullet.GetComponent<RigidbodyProjectile>();
projectile.rigidbody.AddForce(shootPoint.forward.normalized * bulletSpeed, ForceMode.VelocityChange);
return projectile;
}
I don't get it, why my bullet doesn't generated in ShootPoint.transform? I literally removed all components in Bullet and tried again but still had same problem.
Funny thing is that this is not always happening, it only happens in certain angles, especially looking down. I already checked hundreds that ShootPoint pointing something wrong direction or reference of ShootPoint was wrong, but no, there was no problem at all!
What should I check in this case? It's really annoying issue, because when I shoot little lower angle, it always hit the ground almost instantly after fire.
Anyone who knows about this issue, if could be great what should I check, thanks.
** Additional Info about Bullet ** I forgot to mention about bullet. It has parent and child, and parent has rigidbody and child has mesh to display. This is because basically bullet wasn't pointing forward so I rotated it to pointing forward and wrapped by empty game object so that eventually it pointing forward direction even mesh is rotated.
Here's the screenshot to explain this: