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

How would you make a bullet go to the direction of the mouse?

Asked by
tumadrina 179
10 years ago

I know cloning the bullet and stuff like that and GetMuse() but I'm not sure how to use mouse very well.Can I get an explanation or can you refer me somewhere

1 answer

Log in to vote
1
Answered by 10 years ago
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local function computeDirection(vec)
    local lenSquared = vec.magnitude * vec.magnitude
    local invSqrt = 1 / math.sqrt(lenSquared)
    return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt).unit
end

mouse.Button1Down:connect(function()
    if not plr.Character then return end
    local dir = computeDirection(mouse.Hit.p - plr.Character.Torso.Position)
    --'dir' is your direction.  Continue from here.
end)
0
Thank you tumadrina 179 — 10y
Ad

Answer this question