How do you force a tool to shoot in 2D space?

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.

4 Likes

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)

4 Likes

Could you give me an example ?
How do i do this ?

local direction = mouse.Hit.LookVector * Vector3.new(1, 1, 0)

Creates a Vector3 representing the direction of the Mouse’s front face in space with just the x and y coordinates preserved.

1 Like

If it’s a top down shooter you should multiply by Vec3(1, 0, 1)

local direction = mouse.Hit.LookVector * Vector3.new(1, 0, 1)
2 Likes

The solution should be given to @VortexD3V, not me. I just showed a code example using his logic.

1 Like

As of now, it only makes bullet go in set direction, unless i messed up something.

It would help if we could see your code for this. Not the whole thing, just the ray direction variable setting

I was using EtiTheSpirit FastCast Gun because it appears to be the most flexible and feature-rich.
https://www.roblox.com/library/4453914752/FastCast-Redux-Example-Gun

here ist the important part, i think.


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)

on the “Client” script in the gun, replace line 40 with this and it should work (it worked for me):

local FireDirection = (Mouse.Hit.p - FirePointObject.WorldPosition).Unit * Vector3.new(1,0,1)
2 Likes

Thanks man,
You are the reall dream savior :grinning: