I want to make bullets that my gun fires, go at a set angle, or let’s say a certain height where the cursor is pointing.
So far most of the available Toolbox weapons and scripts fire at your cursor, taking into account all 3 axis which is quite problematic for top view shooter because it requires the player to shoot behind or directly at enemies.
I prefer to back engineer tools and scripts to understand them in my own peace, sadly I just could not find any.
So I tried to implement Mouse.X in few tools failing in the process nor I could find any helpful guides explaining how to implement such a feature.
multiply the position by a vector3 so it only goes on 2 axis. For example if you want it to only go on the x + y axis, you would multiply it by vector3.new(1,1,0)
local Handle = Tool:WaitForChild("Handle")
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local MouseEvent = Tool:WaitForChild("MouseEvent")
local Mouse = nil
local ExpectingInput = false
local Camera = workspace.CurrentCamera
local FirePointObject = Handle:WaitForChild("GunFirePoint")
function UpdateMouseIcon()
if Mouse and not Tool.Parent:IsA("Backpack") then
Mouse.Icon = "rbxasset://textures/GunCursor.png"
end
end
function OnEquipped(PlayerMouse)
Mouse = PlayerMouse
ExpectingInput = true
UpdateMouseIcon()
end
function OnUnequipped()
ExpectingInput = false
UpdateMouseIcon()
end
Tool.Equipped:Connect(OnEquipped)
Tool.Unequipped:Connect(OnUnequipped)
UserInputService.InputBegan:Connect(function (input, gameHandledEvent)
if gameHandledEvent or not ExpectingInput then
--The ExpectingInput value is used to prevent the gun from firing when it shouldn't on the clientside.
--This will still be checked on the server.
return
end
if input.UserInputType == Enum.UserInputType.MouseButton1 and Mouse ~= nil then
local FireDirection = (Mouse.Hit.p - FirePointObject.WorldPosition).Unit
MouseEvent:FireServer(FireDirection)
end
end)