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

Am I just dumb or does anyone else not understand this math in guns?

Asked by 2 years ago

Hello. I have this code:

    local mouse = game.Players.LocalPlayer:GetMouse()

    mouse.Button1Down:Connect(function()
        local Bullet = Instance.new("Part")
        Bullet.Parent = game.Workspace
        Bullet.CanCollide = false
        Bullet.CFrame = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame * CFrame.Angles(0,math.rad(90),0)
        local Direction = (mouse.Hit.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Unit
        Bullet.AssemblyLinearVelocity = Direction*100
    end)

The line I am intersted in is:

local Direction = (mouse.Hit.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).Unit

That line and this give the same result:

local Direction = (mouse.Hit.Position).Unit

I don't understand why we need to use the first one? Can anyone explain the math?

0
Math isnt my strongest side so excuse me if i get something wrong here. Basically the first line is creating a unit vector from the Mouse position to the RootPart position. So basically you have a line from the mouse position to the rootpart. Herobrinekid1 31 — 2y

Answer this question