Unity VR Tips — Adding A Rocket Launcher

Scottrlooney
7 min readFeb 7, 2024

--

Aaaaaand we’re back! In our last article we added a sword object to our arsenal and implemented functionality that would treat it as an object with more mass/momentum using the Kinematic option for the Movement Type.

Now we’re going to go up all the way up on the miltary technology ladder and import and configure a rocket launcher. I found a nice example on Sketchfab and configured the materials and textures in Unity once imported. Here it is at the end of the table:

So this will be another two handed object, although in reality it probably is mostly handled by one hand and the other balance point is on the shoulder. However I currently don’t know any way you can mount a weapon on your shoulder at the moment, so for now we’ll have one attach point on the handle in the center and the other hand will attach just in front. Probably not ideal, but it will suffice for now.

Anyway, for this we need a separate rocket object from the launcher itself and amazingly enough that’s what we actually have in this model. So we’re basically good to go. Again, we’ll start with duplicating our Shotgun since it’s two handed interaction. Then we’ll adapt that to this specific model’s needs.

Here’s our collider setup. We’re going to have to have three of these here. One for the launcher tube body, one for the handle area, and one for the rocket itself.

We’ll add these colliders to the XR Grab Interactable script in the Colliders section:

We’ll also set the movement as Kinematic and set Smooth Position and Smooth Rotation.

So the rocket management is handled by two existing scripts, one covering the rocket launcher called RocketLauncherBehavior, and the other one for the rocket itself, the RocketBehavior. Let’s look at the former first:

So this script handles the firing of the rocket prefab using the Activate button event with our controller trigger. It needs a launch point using an empty game object and a rocket prefab. To get the latter we’re going to first unpack our model prefab and then drag our model called SM_RocketNoProjectory as our prefab. After this we add the RocketBehavior script to the prefab and configure it like so.

In this case we dragged a prefab explosion we had in the project but this might need work since in my project it’s missing a script.

Additionally the handling of the rocket firing will be a bit on the tricky side, because the rocket isn’t inside the tube. So we’re likely going to have disable the rocket child object on the Activate event, which is fine. After this the RocketLauncherBehavior script should spawn the rocket prefab , and after it explodes we have to activate the child object on the RPG again to start the whole cycle. We can probably stick an Event Trigger component on the rocket so when it gets destroyed it activates the rocket child object again.

Before we do that, however, we need to configure some explosions and smoke for our rocket prefab since those also aren’t in our project. For this i imported the War FX asset from Jean Moreno (JMO) on the Asset Store.

https://assetstore.unity.com/packages/vfx/particles/war-fx-5669

It’s getting a bit old since it doesn’t support URP or HDRP, only built-in rendering, and it’s using the older Shuriken particle system. but it is FREE, pretty comprehensive, runs on mobile and will definitely suffice. And there’s a ton of stuff here:

Since I only needed an explosion prefab and som flames with smoke I tried importing part of the package but there were too many errors, so overkill it is!

A bit of adjustment on the flame and we have a pretty good version of a rocket flame. And For the explosion I decided on the Nuke effect:

Because why not have more overkill? So I configure the RocketBehavior script to use that Prefab for the Explosion while the Prefab itself will spawn the rocket flame and smoke.

One other fun issue on our rocket model — it’s not facing the right way:

And since a rocket isn’t normally inclined to move sideways, we will need to correct this by utilizing an offset object as a parent and moving our RocketBehavior script to that object instead. It’s easy since there’s no addressing of the rigidbody on the child object.

So for the triggering of the Activate event in our XR Grab Interactable, let’s set this up. We need to talk to our RocketLauncherBehavior script and trigger the FireRocket() function. But we also need to deactivate our static rocket object as well. So we’ve added that in too.

Next, I want to be able to have the destruction of the rocket trigger the reactivation of the the static rocket, and this seemingly simple task blew my whole evening to shreds. To sum up:

A script on a prefab cannot use any reference to a component or object variable in the Hierarchy unless that object is manually connected in the script.

So this means if you need to make a connection to a GameObject from a script on a prefab, you will need to use GameObject.Find() with a string path or use other finding methods in the script. The Inspector will not help you here. And after a few failed tries from the RocketBehavior script to solve the issue where it would work in the Editor but not in the build, I ended up using the RocketLauncherBehavior script instead. Here’s how I solved it:

First we declare two new GameObject variables. One will contain the spawned prefab in the game (the clone), the other contains a reference to the static rocket object under the launcher. We also have a private bool to use as a flag so Update doesn’t start working after Start but before we actually fire the weapon. Let’s check out the firing method:

So this method is slightly altered in that we now assign _rocketClone to the spawned prefab in the scene. After this we set the static rocket child object to false as the prefab rocket clone takes off on its brief mission of destruction. Then we set the _fired boolean to true. Let’s check Update next:

So what this does is continuously monitor the existence of the prefab clone. The flag is there to prevent Update from running the else if code continuously before we have a chance to actually pull the trigger. In reality though it doesn’t do anything except set the static rocket to active continuously. Then when the prefab is destroyed the else if checks the bool to see if we fired then it sets the static rocket to true and resets the bool to false, since we haven’t fired our next round.

Our last step is we need to edit our rocket firing sound. So I’ve done that and updated the prefab with the sound effects. And the rocket prefab has an Audio source that plays on awake as well. You’re not going to hear it in this GIF demo but rest assured — it’s epic!

So now all we need to do is test this in the build (note- this will be with no audio):

So there we are! after persevering we are done with Event #3 and will be moving on to the next one. I would very much like to polish up the interaction here as its FAR less than ideal — especially the sword grabbing. But I’ll save that for a later article and we’ll wrap this one up for now. See you in the next one!

--

--