0
\$\begingroup\$

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:

enter image description here

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:

enter image description here

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:

enter image description here

enter image description here

\$\endgroup\$
  • \$\begingroup\$ Have you double checked the "shootPoint" position and rotation? \$\endgroup\$ – Gabriele Vierti Nov 3 '18 at 13:28
  • \$\begingroup\$ @GabrieleVierti Sure, I checked hundred times. No problem at all. \$\endgroup\$ – modernator Nov 3 '18 at 13:51
  • \$\begingroup\$ I see you are in local transform mode, is the origin of both the bullet and the spawnpoint correct? \$\endgroup\$ – Gabriele Vierti Nov 3 '18 at 13:52
  • 2
    \$\begingroup\$ Does your gun (or character holding it) have a collider? Have you filtered your collisions to ensure the bullet's collider doesn't get repositiined outside of these on its first physics step by collision resolution? \$\endgroup\$ – DMGregory Nov 3 '18 at 15:55
  • 1
    \$\begingroup\$ Fascinating discovery! I'd love to see your findings written up as an answer. :) \$\endgroup\$ – DMGregory Nov 3 '18 at 16:38
0
\$\begingroup\$

I solved this problem, but still don't get it why it happens. If someone know why this happens, could be very helpful with share your information!

Anyway as I mentioned in my question, Bullet object has child object, which is actual object that renders "bullet" mesh. This is because bullet mesh doesn't pointing forward, so I wrapped the bullet mesh and rotated, so the bullet pointing forward without rotation. Also parent object has rigidbody.

However in this game, I'm using Raycast to hit detection because the bullet is really high speed projectile and collider doesn't working well, it just passing through the objects.

But when I was assemble and tested the bullet, I added Mesh Collider into child object. Of course, I turned off that mesh collider after changing colliding logic to using Raycast instead, however, some reason, my problem solved by removing this deactivated mesh collider.

Now bullets spawning correct position, everything works well, but still don't get it why deactivated collider produced this issue.

Anyway if someone facing the similar problem like me, make sure your child object has unusing collider in it. Regardless it's activated or not, it seems like it's affects in physics system.

I hope someday, someone explain this issue with more detail.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.