I have a script and i just want to know how i could make a part shoot in the direction the mouse is facing.
local FireThrown = game.ReplicatedStorage.RemoteEvents.FireThrown
FireThrown.OnServerEvent:Connect(function (plr, spell, spellPosition)
local newSpell = spell:Clone()
newSpell.Parent = game.Workspace
local Fire = newSpell:FindFirstChild("Fire")
local Sphere = newSpell:FindFirstChild("Sphere.001")
if Fire and Sphere then
Sphere.HandleWeld:Destroy()
Fire.Position = spellPosition
Sphere.Position = Fire.Position
end
end)
1 Like
You will need to use a LookVector
. Here’s an example of how to use it.
local Mouse = game.Players.LocalPlayer:GetMouse()
local ShootDirection = Mouse.Hit.LookVector--Mouse Direction
2 Likes
what should i use to make it shoot, assembly linear velocity, linear velocity, or what?
1 Like
If your using a part, the best way would probably be a linear velocity, since it shoots forward, but an even better approach would be using ray casting and make it shoot like that.
In my opinion, raycasting is more reliable and better, and can get more information than a part.Touched
can. Or whatever your using.
hey @JAcoboiskaka1121 ,
i’m not affiliated with OP but if you would be so kind, could you push me in the right direction with what you’d do after a raycast?
For example, I want to make a bullet fire, but it has no relation to the scripts, it’s just for vfx.
I would shoot the ray and handle any code, but then what would I do? Would I use a tween to move a bullet to the position?
2 Likes
Here’s a simple gun script that I used to learn raycasting,
local Tool = script.Parent
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Tool.Activated:Connect(function()
local Origin = Tool.Handle.Position
local Direction = (Mouse.Hit.Position - Origin).Unit
local function CleanUp()
for i, v in pairs(workspace:GetChildren()) do
if v.Name == "RayPart" or v.Name == "OriginPart" then
v:Destroy()
end
end
end
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.FilterDescendantsInstances = {Tool.Handle, Player.Character:GetChildren()}
Params.IgnoreWater = true
local ray = workspace:Raycast(Origin, Direction * 1000, Params)
local RayLine = Instance.new("Part", workspace)
local distance = (ray.Position - Origin).Magnitude
RayLine.Size = Vector3.new(0.2, 0.2, distance)
RayLine.CFrame = CFrame.new(Origin, ray.Position) * CFrame.new(0, 0, -distance/2)
RayLine.Anchored = true
RayLine.Parent = workspace
task.wait(0.5)
RayLine:Destroy()
CleanUp()
end)
If you scroll down to the RayLine Variable, you can see how I did it.
I just used a part to visualise the ray, but depending on what you’re trying to do, you would probably have to tween it or apply a Linear Velocity. What ever is best for your purpose. Maybe beams can also work.
3 Likes
thanks for the help. simple yet effective
3 Likes