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

How do I make body velocity push a brick in the direction the torso is facing?

Asked by 9 years ago

Hi, i was wondering. How do you make it so that the BodyVelocity thing fires out the object in accordance to the Torso ??

Example, the brick is created. You create a BodyVelocity and assign the velocity. This does not work, and I thought it would:

local BodyVelocity = Instance.new("BodyVelocity") BodyVelocity.Parent = power local multiplier = 50 local pos = CFrame.new(0,0,-multiplier) * (torso.CFrame.lookVector * 1) BodyVelocity.velocity = pos

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Credit to UserOnly16Characters for making me explain my code

Credit to Spongocardo for pointing out an error



When I tested that then it worked perfectly, in the correct tool setting of course.

The code I used(localscript in hopperbin in starterpack);


local plr = game.Players.LocalPlayer --Get the player

--Make a function to add force to the object, relative to the torso
function push(obj,torso)
    local BodyVelocity = Instance.new("BodyVelocity",obj)
    local multiplier = 50 --How much force to push them by
    --Mulitply the force on the negative Z axis(pushing back), from the torso's lookVector
    local pos = CFrame.new(0,0,-multiplier) * (torso.CFrame.lookVector * 1) 
    --Apply the force
    BodyVelocity.velocity = pos
end

--When the hopperbin is selected, or if it was a tool the term would be 'Equipped'
script.Parent.Selected:connect(function(mouse)
    --When they left click
    mouse.Button1Down:connect(function()
        --Get the part their mouse is on
        local brick = mouse.Target
        --Check if the brick is anchored or not
        if not brick.Anchored then
            --Call the function to add the BodyVelocity
            push(brick,plr.Character.Torso)
        end
    end)
end)

And perhaps you could credit me next time you decide to post code of mine(:

2
It would be nice if you could explain how the script works so people who are new to SH could learn from it. UserOnly20Characters 890 — 9y
0
It's the same script, just in a tool setup. Goulstem 8144 — 9y
0
But you're right(: Goulstem 8144 — 9y
1
I think you were meant to comment on line 3. :P Spongocardo 1991 — 9y
0
Thx Goulstem 8144 — 9y
Ad

Answer this question