Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make rocket launcher fire in the direction someone is looking, not where their mouse is?

So I'm trying to make a rocket launcher work for mobile users since when you walk and shoot at the same time, the rocket shoots to the part of the screen you're touching to move. I made it so that when you equip the rocket, it checks if you're on mobile or not and if you are, it forces you into shift lock.

Now I just need to figure out how to make it so that when you click the screen, it shoots at wherever you're looking.

Here's the script for the rocket as it is (it's just a modified version of the ROBLOX model rocket launcher, also the force into shift lock thing is in another script within the rocket.)

Please help!

-----------------
--| Constants |--
-----------------

local GRAVITY_ACCELERATION = workspace.Gravity

local RELOAD_TIME = .8 -- Seconds until tool can be used again
local ROCKET_SPEED = 150 -- 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 Tool = script.Parent
local ToolHandle = Tool:WaitForChild("Handle")

local MyPlayer = PlayersService:GetPlayerFromCharacter(Tool.Parent.Parent.Character)

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
	Rocket.Material = Enum.Material.Neon
	
	local rocketScriptClone = RocketScript:Clone()
	rocketScriptClone.Parent = Rocket
	rocketScriptClone.Disabled = 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
	fire.SecondaryColor = Color3.new(255,255,255)
	
	-- 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
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
		rocketClone.Fire.Color = MyPlayer.TeamColor.Color

		-- 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



--------------------
--| Script Logic |--
--------------------
Tool.Activated:Connect(OnActivated)

1 answer

Change line 96 to

rocketClone.CFrame = CFrame.new(spawnPosition, tool.Parent.Head.CFrame.LookVector)

The tool is in the character (When equipped), so the head is accessible. The head's CFrame contains, like all CFrames, a lookVector. A lookVector is the direction that a CFrame is facing, so now it will shoot to where the player's head faces, which is the direction that a player is looking.

You can also try with the HumanoidRootPart's CFrame.

6 comments

0

tell me if it doesn't work, but it should...

0

It still fires at wherever I click

0

I also tried HumanoidRootPart's CFrame

0

Are you testing on mobile? 

0

I might have another answer, if so

0

nvm

This discussion is preserved as it was. New answers, comments, and votes are closed.