How to fire a projectile towards mouse in first person?

Hello! Today I am here to ask about something I have been thinking about for my game. I am making a game with slingshots and I need to create a script for firing the “Pellets”. Can somebody please explain how this is done? Here’s my 2 scripts so far:

Local script

local Mouse = game.Players.LocalPlayer:GetMouse()
local RE = script.Parent:WaitForChild("ClassicFire")

Mouse.Button1Down:Connect(function()
	RE:FireServer(Mouse.Hit.Position)
end)

Server Script

local Slingshot = script.Parent.Parent
local Handle = Slingshot.Handle
local Equipped = false
local Pellet = game.ServerStorage.Pellets.ClassicPellet
local RE = script.Parent:WaitForChild("ClassicFire")
local debounce = false

Slingshot.Equipped:Connect(function()
	Equipped = true
end)

Slingshot.Unequipped:Connect(function()
	Equipped = false
end)

local function OnFire(player, MousePos)
	if Equipped == true then
		if debounce == false then
			debounce = true
			local ClonedPellet = Pellet:Clone()
			--stuck here
		end
	end
end

RE.OnServerEvent:Connect(OnFire())

There are probably so many problems with this already… Thanks for reading! (P.S: I know I am messy :expressionless:)

In first person the mouse is locked to the center of screen by default, you changed that? If you didnt you can use the Camera LookVector.

If in your game mouse is not locked and can be moved while in first person, then you can get the direction where the mouse is relative to the camera with this

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local lookVector = mouse.UnitRay.Direction

It uses the Camera origin (which is centered) and it consider where the mouse is positioned to get the direction line for a projectile to go into that direction. Im bad at explaining… heres the docu

Ok, I just read through your response and I understand it mostly. How could I then send the projectile in that direction?

How you wanna move the pellet? by using Physics? by changing the CFrame? Tweening?
Its an actual model/part? I had to search for the word “pellet” sorry english is not my native language… xD It just says its a “bolita”… and I dont quite get it

If you want to move it by using the physics, go for MoversConstraints or BodyMovers

:ApplyImpulse() will be your friend. Physics is always better for this.

1 Like

Solved! I just followed a yt tutorial! I need to go so I won’t share the link sorry just search up “How to make gun in roblox studio” on youtube!

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