Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this mobile attack button make weapons fire in the camera's direction?

Asked by 6 years ago
Edited 6 years ago

Edit: After some looking around, I've realized that if I want to get the result I'm looking for, I'm gonna have to change the weapons themselves, rather than a single script. What I think I need to know now is how to get the position the camera is looking at so I can get the ranged weapons to fire there. Perhaps I could get it with raycasting?

I'm working on a game with plenty of ranged weapons, and I want to make them easier to use on mobile. I've started work on a script that makes an attack button appear whenever you equip a weapon on mobile. It works as it's supposed to... Kinda. It does activate the weapon whenever you tap the button, but it makes ranged weapons fire towards where you tapped, which, of course, is on the button, which is in the corner of the screen. I'd like to know how to make it so tapping the attack button makes ranged weapons fire in the direction the camera is facing, rather than towards the corner of the screen where the button is.

Here's my code:

local player = game.Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
    character = player.CharacterAdded:wait()
end

local uis = game:GetService("UserInputService")
local ces = game:GetService("ContextActionService")

--[[if uis.TouchEnabled then
    game:GetService("RunService").RenderStepped:Connect(function()
        uis.MouseBehavior = Enum.MouseBehavior.LockCenter
        uis.MouseIconEnabled = true
    end)
end]]-- a failed attempt at making it work right

local function activateweapon()
    if character:FindFirstChildOfClass("Tool") then
        character:FindFirstChildOfClass("Tool"):Activate()
    end
end

character.ChildAdded:Connect(function(child)
    if child:IsA("Tool") then
        child.ManualActivationOnly = true
        ces:BindAction("Attack", activateweapon, true)
        ces:SetTitle("Attack", "Attack")
    end
end)

character.ChildRemoving:Connect(function(child)
    if child:IsA("Tool") then
        child.ManualActivationOnly = false
        ces:UnbindAction("Attack")
    end
end)

Please help me out here, I want to make mobile gameplay not terrible!

0
You could fix this more easily to just detect when the players screen is pressed? User#20388 0 — 6y
0
You can make the projectile fire where the player's character is facing (lookVector) awesomeipod 607 — 6y
0
@RedcommanderV2 That's not what I want to do though. I want to have a button that makes weapons fire, like in a lot of mobile shooter games. WesleyTheSkunk 12 — 6y
0
@awesomeipod That wouldn't help much, imo it would actually limit mobile gameplay more. You would have to move to face towards whoever you want to attack, and what if they're above or below you? It wouldn't work. WesleyTheSkunk 12 — 6y

Answer this question