![](https://csdnimg.cn/release/download_crawler_static/71118581/bg5.jpg)
5
This code ensures that the weapon cant fire faster than reloadTime . It also checks that
the user can fire only when they have sufficient ammo.
The behavior for the RocketLauncher is similar to that in the previous FPS tutorial with
the exception of the reload time and ammo count described above.
RocketLauncher FPS
Attach the RocketLauncher.js script to the RocketLauncher game object. Make sure
that you are not attaching this script to the rocketLauncher child game object.
RocketLauncher.js RocketLauncher
rocketLauncher
Rocket
We will now build the rocket in the scene and upload the final version to a prefab.
Click on Objects/weapons/rocket in the Project window and make sure that the
FBXImporter Scale Factor is set to 1, otherwise the model will import at a very small
scale.
Objects/weapons/rocket FBXImporter Scale Factor
1
Drag the Objects/weapons/rocket model into the Scene view.
Objects/weapons/rocket
Attach the
WeaponScripts/Rocket.js
script to it.
WeaponScripts/Rocket.js
Add a box collider to the rocket game object. Make the box collider slightly larger
than the actual rocket to prevent tunneling of collisions. Tunneling of collisions is
what happens when small, fast game objects avoid collision detection due to their size
and speed. Making the box collider z-axis larger ensures collisions work correctly for
instantiatedProjectile.velocity = transform.TransformDirection(
Vector3 (0, 0, initialSpeed));
// Ignore collisions between the missile and the character controller
//
//
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}