How to make rocket launcher projectile spawn at camera position

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hey, I am trying to alter the classic rocket launcher’s code to spawn the projectile at the players camera.

I have tried using remote events and remote functions to pass through a variable with the camera’s Cframe data but I have been unable to make it function.
I have provided some images on what I am trying to achieve. (there is also a place download)

-- server script 
-----------------
--| Constants |--
-----------------

local GRAVITY_ACCELERATION = workspace.Gravity

local RELOAD_TIME = 3 -- Seconds until tool can be used again
local ROCKET_SPEED = 60 -- Speed of the projectile

local MISSILE_MESH_ID = 'http://www.roblox.com/asset/?id=2251534'
local MISSILE_MESH_SCALE = Vector3.new(0.35, 0.35, 0.25)
local ROCKET_PART_SIZE = Vector3.new(1.2, 1.2, 3.27)

-----------------
--| Variables |--
-----------------

local DebrisService = game:GetService('Debris')
local PlayersService = game:GetService('Players')

local MyPlayer

local Tool = script.Parent
local ToolHandle = Tool:WaitForChild("Handle")

local MouseLoc = Tool:WaitForChild("MouseLoc",10)

local RocketScript = script:WaitForChild('Rocket')
local SwooshSound = script:WaitForChild('Swoosh')
local BoomSound = script:WaitForChild('Boom')

--NOTE: We create the rocket once and then clone it when the player fires
local Rocket = Instance.new('Part') do
	-- Set up the rocket part
	Rocket.Name = 'Rocket'
	Rocket.FormFactor = Enum.FormFactor.Custom --NOTE: This must be done before changing Size
	Rocket.Size = ROCKET_PART_SIZE
	Rocket.CanCollide = false

	-- Add the mesh
	local mesh = Instance.new('SpecialMesh', Rocket)
	mesh.MeshId = MISSILE_MESH_ID
	mesh.Scale = MISSILE_MESH_SCALE

	-- Add fire
	local fire = Instance.new('Fire', Rocket)
	fire.Heat = 5
	fire.Size = 2

	-- Add a force to counteract gravity
	local bodyForce = Instance.new('BodyForce', Rocket)
	bodyForce.Name = 'Antigravity'
	bodyForce.Force = Vector3.new(0, Rocket:GetMass() * GRAVITY_ACCELERATION, 0)

	-- Clone the sounds and set Boom to PlayOnRemove
	local swooshSoundClone = SwooshSound:Clone()
	swooshSoundClone.Parent = Rocket
	local boomSoundClone = BoomSound:Clone()
	boomSoundClone.PlayOnRemove = true
	boomSoundClone.Parent = Rocket

	-- Attach creator tags to the rocket early on
	local creatorTag = Instance.new('ObjectValue', Rocket)
	creatorTag.Value = MyPlayer
	creatorTag.Name = 'creator' --NOTE: Must be called 'creator' for website stats
	local iconTag = Instance.new('StringValue', creatorTag)
	iconTag.Value = Tool.TextureId
	iconTag.Name = 'icon'

	-- Finally, clone the rocket script and enable it
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	rocketScriptClone.Disabled = false
end

-----------------
--| Functions |--
-----------------

local function OnActivated()
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)
		-- Create a clone of Rocket and set its color
		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = MyPlayer.TeamColor

		-- Position the rocket clone and launch!
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(5, 0, 0)).p
		rocketClone.CFrame = CFrame.new(spawnPosition, Pos) --NOTE: This must be done before assigning Parent
		rocketClone.Velocity = rocketClone.CFrame.lookVector * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
		rocketClone:SetNetworkOwner(nil)

		wait(RELOAD_TIME)

		Tool.Enabled = true
	end
end

function OnEquipped()
	MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent)
end

--------------------
--| Script Logic |--
--------------------

Tool.Equipped:Connect(OnEquipped)
Tool.Activated:Connect(OnActivated)


-- Client Script
local Tool = script.Parent

local Remote = Tool:WaitForChild("MouseLoc")

local Mouse = game.Players.LocalPlayer:GetMouse()

function Remote.OnClientInvoke()
	return Mouse.Hit.p
end

rocketlauncher.rbxl (64.0 KB)

2 Likes

Where does the rocket spawn when the tool is activated?
If you don’t know how the get the camera then you can use this

local cam = workspace.CurrentCamera
cam.CFrame.Position--- Use this to get the camera's Position in the world
2 Likes

it spawns on the tip of the tool I will provide the function where it is stated

local function OnActivated()
	local myModel = MyPlayer.Character
	if Tool.Enabled and myModel and myModel:FindFirstChildOfClass("Humanoid") and myModel.Humanoid.Health > 0 then
		Tool.Enabled = false
		local Pos = MouseLoc:InvokeClient(MyPlayer)
		-- Create a clone of Rocket and set its color
		local rocketClone = Rocket:Clone()
		DebrisService:AddItem(rocketClone, 30)
		rocketClone.BrickColor = MyPlayer.TeamColor

		-- Position the rocket clone and launch!
		local spawnPosition = (ToolHandle.CFrame * CFrame.new(5, 0, 0)).p
		rocketClone.CFrame = CFrame.new(spawnPosition, Pos) --NOTE: This must be done before assigning Parent
		rocketClone.Velocity = rocketClone.CFrame.lookVector * ROCKET_SPEED --NOTE: This should be done before assigning Parent
		rocketClone.Parent = workspace
		rocketClone:SetNetworkOwner(nil)

		wait(RELOAD_TIME)

		Tool.Enabled = true
	end
end
1 Like

Are you using velocity to move the rocket?
And when you spawn the rocket does it face the mouse’s position?

2 Likes

yes it is using body force, it faces the target provided by the mouses position

1 Like

If you want the rocket to spawn on the camera’s position, then use the code I pasted above and replace it with the spawn code of your rocket

1 Like

where should put this code as it doesn’t work in a server script?

1 Like

Is this a local script?

Filler…-

this is a server script and has a localscript feeding the mouse position into it

Since this is a server script you’ll have to get the player’s camera position from the local script just like the mouse position. Once you have done that the code should look something like this

local spawnPosition = CameraPosVariable --- This would be that position you got from the local script
rocketClone.CFrame = CFrame.new(spawnPosition, Pos) --NOTE: This must be done before assigning Parent

are you able to walk me through how I can make this adjustment? here is the local script

local Tool = script.Parent

local Remote = Tool:WaitForChild("MouseLoc")

local Mouse = game.Players.LocalPlayer:GetMouse()

function Remote.OnClientInvoke()
	return Mouse.Hit.p
end

Okay, one thing can you fill me in on is. What does the Remote.OncClientInvoke Do, Since I don’t use that and instead use RemoteEvents.
Very sorry about this.

I believe it gets called by the server script and performs the function returning the mouse position

And what about the variable local Remote = Tool:WaitForChild("MouseLoc")
Where does this go to?

It goes to a remote function

Okay, thanks.

local Tool = script.Parent

local Remote = Tool:WaitForChild("MouseLoc")

local Mouse = game.Players.LocalPlayer:GetMouse()

local Camera = workspace.CurrentCamera

function Remote.OnClientInvoke()
	return Mouse.Hit.p,Camera.CFrame.Position
end

Something like this should give the server script the information it needs.
This will give a table so make sure you select the 2nd variable in that table.

2 Likes

thank you very much this worked!

Glad I could solve your problem!

Good luck making your game/prototype.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.