PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Making a Guided Rocket Launcher

Making a Guided Rocket Launcher

Jul 03 2018, 10:36 AM PST 10 min

This tutorial explains how to modify existing code to make a rocket that the camera follows and can be controlled after launch.

Getting started

Before you begin work on the new rocket, you’ll need to do the following:

  1. Open Roblox Studio
  2. Create a new place
  3. Go to Toolbox > Inventory > Weapons and double-click the rocket launcher to add it to the starter pack.

Modifying the code

The RocketLauncher model has two scripts:

  • RocketLauncherScript
  • LocalGUI

Since we are modifying the behavior of the rocket and not the GUI or appearance, we will be working with RocketLauncherScript.

The entire script can look a little overwhelming at first. It’s helpful to realize that the script is already broken into smaller functions, each of which is not as intimidating when separated from the rest of the code. These functions are listed below:

  • blow(hit, missile)
  • onPlayerBlownUp(part, distance, creator)
  • tagHumanoid(humanoid, creator)
  • fire(vTarget)
  • computeDirection(vec)
  • onActivated()

Now that we have identified the separate parts of RocketLauncherScript, let’s identify which parts we will be modifying. For this, we can simply look at the names of the functions, which usually describe their purposes. Since we want to change the behavior of the rocket once it is launched, it looks like we’ll be modifying the fire(vTarget) function and maybe blow(hit, missile) to restore the initial behavior of the rocket when it explodes.

Now that we know where we’ll add our changes, let’s outline what we want our new rocket script to do. Below are the actions we want to perform:

  • Make the camera follow the rocket
  • Control the rocket after it’s launched

Make the camera follow the rocket

In order to have the camera follow the rocket, we will need to interact with the Camera object. Thankfully, there’s already a tutorial on Articles/Camera Manipulation that shows how to do this! Below is a snippet of code from the tutorial that changes the subject of the camera to a certain part in the workspace:

local cam = workspace.CurrentCamera
cam.CameraSubject = workspace.Part

That sounds exactly like what we want to do! The only thing we want to do now is change the camera type to “Follow” so that the camera follows the rocket. Fortunately, this piece of code is also included in the tutorial. Here it is below:

local cam = workspace.CurrentCamera
cam.CameraType = "Attach"

We can easily adapt these two code snippets to what we want to do. First, let’s take a look at the function we’ll be modifying:

function fire(vTarget)
	local vCharacter = Tool.Parent	
	local vHandle = Tool:findFirstChild("Handle")
	if vHandle == nil then
		print("Handle not found")
		return 
	end
	local direction = vTarget - vHandle.Position
	direction = computeDirection(direction)
	local missile = Rocket:Clone()
	local pos = vHandle.Position + (direction * 10.0)		
	missile.CFrame = CFrame.new(pos,  pos + direction) * CFrame.Angles(math.pi/2, 0, 0)

	local creator_tag = Instance.new("ObjectValue")

	local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)

	if vPlayer == nil then
		print("Player not found")
	else
		if (vPlayer.Neutral == false) then -- nice touch
			missile.BrickColor = vPlayer.TeamColor
		end
	end

	local floatForce = Instance.new("BodyForce")
	floatForce.force = Vector3.new(0, missile:GetMass() * 196.1, 0.0)
	floatForce.Parent = missile

	missile.Velocity = direction * 20.0

	creator_tag.Value = vPlayer
	creator_tag.Name = "creator"
	creator_tag.Parent = missile	

	missile.Parent = game.Workspace

	if swooshSound then swooshSound:Play() end

	missile.Touched:Connect(function(hit) blow(hit, missile) end)

	debris:AddItem(missile, 100.0)
end

As you can see, the variable missile represents the rocket that is being launched. That means that we want missile to be the Camera/CameraSubject|Camera Subject. Below is our updated code (also with “Follow” instead of “Attach”):

local cam = workspace.CurrentCamera
cam.CameraSubject = missile
cam.CameraType = "Follow"

Add the above code to the end of the fire(vTarget) function.

Great! Now the camera follows the rocket once it’s launched. The only problem is that, once the rocket explodes, the camera is stuck at the point where the rocket exploded. That means that we’ll need to restore the camera back to normal in the blow(hit, missile) function. Once again, the camera manipulation tutorial gives us the code we need to do this

Add the below code into the beginning of blow(hit, missile).

game.Workspace.CurrentCamera.CameraSubject = game.Players.LocalPlayer.Character.Humanoid
game.Workspace.CurrentCamera.CameraType = "Custom"

Control the rocket after launch

Now comes the more difficult part. There is no tutorial for redirecting a rocket’s velocity, so we must do it ourselves. If we look in the fire(vTarget) function above, we can see the following line of code:

missile.Velocity = direction * 20.0

This code sets the velocity (direction and speed) of the missile. That means we need to change missile.Velocity to the direction the mouse is pointing while the rocket is in motion. First, we’ll need to see how the variable direction was created and then reuse that process. If we backtrack through the code and look for lines of code involved with the variable direction, we see the following code:

--In onActivated() function
local character = Tool.Parent;
local humanoid = character.Humanoid
local targetPos = humanoid.TargetPoint

--In fire() function
--targetPos becomes vTarget here
local vHandle = Tool:findFirstChild("Handle")
local direction = vTarget - vHandle.Position
direction = computeDirection(direction)
missile.Velocity = direction * 20.0

If we substitute all the variables from previous lines, we get the following line of code:

missile.Velocity = computeDirection(Tool.Parent.Humanoid.TargetPoint - Tool.findFirstChild("Handle").Position) * 20.0

With this code, it is easier to see how missile.Velocity is assigned. Since Velocity is a vector, we can see that the direction is calculated by taking the Position of the weapon handle and subtracting it from the position of the TargetPoint (where the mouse is pointing). This subtraction operation creates a vector pointing from the handle of the weapon to the target.

Now, let’s apply the same principles used in the starting code to make the rocket controllable after it is launched. Each 30th of a second (any small interval would work) we want to change missile.Velocity to the vector pointing from the rocket to the target. Notice that our target is the same (wherever the mouse is pointing) but our starting point is different (position of missile instead of handle). That means we need to change the line of code we derived to subtract missile.Position from the TargetPoint.

Our line of code looks like this now:

missile.Velocity = computeDirection(Tool.Parent.Humanoid.TargetPoint - missile.Position) * 20.0

Now, let’s make a loop to execute this line. Since we want the looping to stop when blow(hit, missile) is called, we’ll need to declare a variable (let’s call it active) in the top of the script, set it to true when fire(vTarget) is called and set it to false when blow(hit, missile) is called. Then, we can use a while loop with our variable to keep executing our line of code until the rocket explodes. Lastly, we need to include a wait() so that the loop pauses in between each execution (otherwise the game would crash!)

Our final modification code is below:

local active

Insert the above code at the beginning of the script.

active = true
while(active) do
	direction =  computeDirection(Tool.Parent.Humanoid.TargetPoint - missile.Position)
	missile.Velocity = direction * 20.0
	
	--The below line of code wasn't in the tutorial.  It's optional and just points the missile in the right direction
	missile.CFrame = CFrame.new(missile.Position,  missile.Position + direction) * CFrame.Angles(math.pi/2, 0, 0)
	wait()
end

Insert the above code at the end of fire(vTarget)

active = false

Insert the above code at the beginning of blow(hit, missile).

Make it Work Online

In order to make the guided rocket work properly online, you need to copy the entire main script and paste it into a LocalScript in the same location as the original script.

Tags:
  • weapons
  • coding