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

How do I apply direction to a velocity?

Asked by 9 years ago

I want to make a part fly off towards the direction it's facing when a player touches it. I can get the direction with lookVector, but how would I apply that to something, like BodyVelocity?

3 answers

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

Velocity is both the speed and the direction of a Part. lookVector is also a direction, so you can set it directly:

part.Velocity = part.CFrame.lookVector * bigNumber

You need to multiply it by a big number so that it will have more speed.

Ad
Log in to vote
1
Answered by 9 years ago

Here's some code for a missle from a plane - basically BodyVelocity.velocity is set to the lookvector of a part and multiplied by the speed you want it to go forward.

function FireMissile(Gun) --This function creates the non-guided missiles for the missile spawns
    if (not Locked) then
        local Exploded = false
        local Part1 = Instance.new("Part")
        Part1.Name = "Missile"
        Part1.CanCollide = false
        Part1.FormFactor = "Symmetric"
        Part1.Size = Vector3.new(1,1,5)
        Part1.BottomSurface = "Smooth"
        Part1.TopSurface = "Smooth"
        local Mesh = Instance.new("SpecialMesh")
        Mesh.Parent = Part1
        Mesh.MeshId = "http://www.roblox.com/asset/?id=2251534"
        Mesh.MeshType = "FileMesh"
        Mesh.TextureId = "http://www.roblox.com/asset/?id=2564491"
        local Part2 = Instance.new("Part")
        Part2.Parent = Part1
        Part2.Name = "Visual"
        Part2.Transparency = 1
        Part2.CanCollide = false
        Part2.FormFactor = "Symmetric"
        Part2.Size = Vector3.new(1,1,1)
        Part2.BottomSurface = "Smooth"
        Part2.TopSurface = "Smooth"
        local Weld = Instance.new("Weld")
        Weld.Parent = Part1
        Weld.Part0 = Part1
        Weld.Part1 = Part2
        Weld.C0 = CFrame.new(0,0,5)*CFrame.Angles(math.rad(0),0,0)
        local BV = Instance.new("BodyVelocity")
        BV.Parent = Part1
        BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
        local BG = Instance.new("BodyGyro")
        BG.Parent = Part1
        BG.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
        BG.cframe = Gun.CFrame * CFrame.Angles(math.rad(0),0,0)
        local Fire = Instance.new("Fire")
        Fire.Parent = Part2
        Fire.Enabled = false
        Fire.Heat = 25
        Fire.Size = 30
        local Smoke = Instance.new("Smoke")
        Smoke.Parent = Part2
        Smoke.Color = Color3.new(40/51,40/51,40/51)
        Smoke.Enabled = false
        Smoke.Opacity = 1
        Smoke.RiseVelocity = 25
        Smoke.Size = 25
        local PlaneTag = Instance.new("ObjectValue")
        PlaneTag.Parent = Part1
        PlaneTag.Name = "PlaneTag"
        PlaneTag.Value = Plane
        Part1.Touched:connect(function(Object)
            if (not Exploded) then
                if (not Object:IsDescendantOf(Character)) and Object.Name ~= "Missile" then
                    Exploded = true
                    local Explosion = Instance.new("Explosion")
                    Explosion.Position = Part1.Position
                    Explosion.BlastPressure = 50
                    Explosion.BlastRadius = 5
                    ScanPlayers(Part1.Position,5)
                    Explosion.Parent = game.Workspace
                    Part1:Destroy()
                end
            end
        end)
        Part1.Parent = game.Workspace
        Part1.CFrame = (Gun.CFrame * CFrame.Angles(math.rad(0),0,0)) + Gun.CFrame.lookVector * 15
        BV.velocity = Part1.CFrame.lookVector * Engine.Velocity.magnitude
        delay(.03,function()
            BV.velocity = (Part1.CFrame.lookVector * 1000) + Vector3.new(0,0,0)
            Fire.Enabled = true
            Smoke.Enabled = true
        end)
        delay(5,function() Part1:Destroy() end)
    end
end
Log in to vote
0
Answered by 9 years ago

Speed and Direction and both controlled by the velocity. here's and example

function fire(d)
Part.Position = script.Parent.Part.Position + (d*5)
Part.Velocity = d*100
end

so the *5 is how far it will be pushed from the part(not how far overall) and *100 is how fast it will be pushed higher the number faster it goes.

Answer this question