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

How to make BodyVelocity more accurate?

Asked by
neoG457 315 Moderation Voter
8 years ago
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

enabled = true
Mouse.KeyDown:connect(function(key)
if not enabled then
        return
    end
if key == "z" then

        local Shot = Instance.new("Part", workspace)
        Shot.Name          = "Shot"
        Shot.BrickColor    = BrickColor.new("White")
        Shot.Transparency  = 0
        Shot.Anchored      = false
        Shot.CanCollide    = false
        Shot.TopSurface    = Enum.SurfaceType.SmoothNoOutlines
        Shot.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
        Shot.Shape         = ("Ball")
        Shot.Size          = Vector3.new(2, 2, 2)
        Shot.CFrame        = Player.Character.Torso.CFrame*CFrame.new(0,0,0)
        Shot.formFactor    = Enum.FormFactor.Custom

        local AirEffect     = Instance.new("Part", Shot)
        AirEffect.Name          = "Shot"
        AirEffect.BrickColor    = BrickColor.new("White")
        AirEffect.Transparency  = 0.5
        AirEffect.Anchored      = false
        AirEffect.CanCollide    = false
        AirEffect.TopSurface    = Enum.SurfaceType.SmoothNoOutlines
        AirEffect.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
        AirEffect.Shape         = ("Ball")
        AirEffect.Size          = Vector3.new(2.5, 2.5, 2.5)
        AirEffect.formFactor    = Enum.FormFactor.Custom
        AirEffect.CFrame        = Player.Character.Torso.CFrame*CFrame.new(0,0,0)       

        local W = Instance.new("Weld")
        W.Part0 = AirEffect
        W.Part1 = Shot
        W.Parent = AirEffect
        W.C0 = CFrame.Angles(0, 0, 0)   

local Woosh = Instance.new("BodyVelocity")
Woosh.maxForce = Vector3.new(math.huge, math.huge, math.huge)
Woosh.velocity = Vector3.new(Mouse.Hit.x, Mouse.Hit.y, Mouse.Hit.z)
Woosh.Parent = Shot



end
end) 

This Local Script is for a ranged attack. When you press Z it fires a shot to where your mouse is pointing. It works fine however its extremely inaccurate. Please help me improve this.

This is how inaccurate it is: https://gyazo.com/cb7ac35954413ee8c3c866a0a49fa2ca

1 answer

Log in to vote
2
Answered by 8 years ago

Your problem isn't that it's inaccurate, you setting the velocity to the position where you click. Velocity is speed with a direction. It doesn't go to a position if you give it a position.

local speed = 500 --add a variable speed in your code, it's how fast woosh moves

Woosh.velocity = CFrame.new(Player.Character.Torso.Position, Mouse.Hit).lookVector * speed --this is line 45

When you create a cframe with two vector3s it creates a CFrame with the position of the first vector, rotated to face the second. The lookVector of the cframe is the front facing direction of the cframe (it's a normalized vector, it's magnitude is between 0 and 1)

This is kind of advanced and I taught myself this math so it's kind of hard to explain. But I hope you got the gist of it

Your finished code

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local speed = 500
enabled = true
Mouse.KeyDown:connect(function(key)
    if not enabled then
       return
    end 
    if key == "z" then

        local Shot = Instance.new("Part", workspace)
        Shot.Name          = "Shot"
        Shot.BrickColor    = BrickColor.new("White")
        Shot.Transparency  = 0
        Shot.Anchored      = false
        Shot.CanCollide    = false
        Shot.TopSurface    = Enum.SurfaceType.SmoothNoOutlines
        Shot.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
        Shot.Shape         = ("Ball")
        Shot.Size          = Vector3.new(2, 2, 2)
        Shot.CFrame        = Player.Character.Torso.CFrame*CFrame.new(0,0,0)
        Shot.formFactor    = Enum.FormFactor.Custom

        local AirEffect     = Instance.new("Part", Shot)
        AirEffect.Name          = "Shot"
        AirEffect.BrickColor    = BrickColor.new("White")
        AirEffect.Transparency  = 0.5
        AirEffect.Anchored      = false
        AirEffect.CanCollide    = false
        AirEffect.TopSurface    = Enum.SurfaceType.SmoothNoOutlines
        AirEffect.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
        AirEffect.Shape         = ("Ball")
        AirEffect.Size          = Vector3.new(2.5, 2.5, 2.5)
        AirEffect.formFactor    = Enum.FormFactor.Custom
        AirEffect.CFrame        = Player.Character.Torso.CFrame*CFrame.new(0,0,0)       

        local W = Instance.new("Weld")
        W.Part0 = AirEffect
        W.Part1 = Shot
        W.Parent = AirEffect
        W.C0 = CFrame.Angles(0, 0, 0)   

        local Woosh = Instance.new("BodyVelocity")
        Woosh.maxForce = Vector3.new(math.huge, math.huge, math.huge)
        Woosh.velocity = CFrame.new(Player.Character.Torso.CFrame.p, Mouse.Hit.p).lookVector * speed
        print(CFrame.new(Player.Character.Torso.CFrame.p, Mouse.Hit.p).lookVector)
        Woosh.Parent = Shot

    end
end) 

1
I'm in love with you. neoG457 315 — 8y
1
i ship it drew1017 330 — 8y
1
I'm okay with this YellowoTide 1992 — 8y
Ad

Answer this question