Bullets Muse
Today was the day I finally crushed the problem of the weapons not shooting like normal. For two days I wrestled with the problem of rewriting the weapon code so that the bullet, zoid, weapon all remained as separate entities so that I could mix and match them later on for easy user customization.
I converted two scripts, including the code in my previous post, Player_Zoid_Control, and bullet_control into three other scripts, Weapon_Handle, Bullet_Handle, Bullet_Mech, so that the zoid, weapon, bullet respectively can do their own stuff, just exchanging state messages to communicate. It sounded great to me, except it won’t shoot.
I logged the OnRayHit event and found the bullet hitting the floor most of the time. Other times it shot itself in the foot. Sometimes it flew to who knew where and all I could see was a fleck of dust off the screen. What the heck?
I used the following code, modified from the CameraFollow.js from Standard Assets to make the bullet shoot straight.
#Somewhere inside Bullet_Mech
#missrotate is some random Quartenion to add some random
#fun to the shooting
unitvec = (missrotate*self.transform.rotation)*Vector3.forward
self.rigidbody.velocity=unitvec* speed
It worked perfectly with the zoid movement controls, so there wasn’t a reason it shouldn’t work with the bullet, so there was something fishy here.
I reverted to the original scripts and bullets before testing them out. It worked fine. Then I went through the old code again and saw this.
#Somewhere inside Player_Zoid_Control
#Adjust for running while firing:
if self.rigidbody.velocity.magnitude>14:
offset=3.5
else:
offset=2
What the-? Then I remembered vaguely a year ago when I wrote this hack, somehow the bullets keep hitting the head and I had to move the bullet instantiating point forward…
Then it hit me. Like a truck.
I double checked the position of the weapon on the zoid and the box collider of the head of the zoid. Sure enough, the weapon was just behind the diagonal face of the collider and when the bullet was shot, it will first hit the head, the go straight down.
Sighing, I reverted all changes to back to the new code and simply moved the weapon ahead of the head collider. Easy solution to a lot of mayhem.
Bang! goes the bullet. Finally.