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

How to use body forces and body gyro correctly?

Asked by 8 years ago

Hey guys I've been playing around with body forces and body gyros for a few hours not to no success. I want a brick to go to where i click gradually and stop as soon as it reaches where i click.

Currently i have a system which causes the brick to move to where i want but wont stop till after the point as it needs to slow down and its also currently rotating like crazy, i want it to face the direction it is traveling.

Below is my code.

local UIS = game:GetService("UserInputService") 
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local speed = 20
local selectedUnit = nil 

UIS.InputBegan:connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        if mouse.Target then 
            if mouse.Target.Name == "Unit" then 
                selectedUnit = mouse.Target
            elseif mouse.Hit then  
                if selectedUnit then  
                    print("move unit")
                    --selectedUnit.CFrame = mouse.Hit
                    print(selectedUnit:GetChildren())
                    selectedUnit.BodyGyro.CFrame = CFrame.new(selectedUnit.Position*Vector3.new(1,0,1), mouse.Hit.p*Vector3.new(1,0,1))
                    selectedUnit.BodyVelocity.Velocity = CFrame.new(selectedUnit.Position, mouse.Hit.p).lookVector * speed
                    repeat wait(0.5) until (selectedUnit.Position - mouse.Hit.p).magnitude < 3
                    selectedUnit.BodyVelocity.Velocity = Vector3.new(0,0,0)

                    selectedUnit = nil
                end
            end
        end
    end
end)  

Any suggestions, improvements, tips or even a complete re-haul of what i'm attempting would be appreciated.

0
i wouldnt know how to do it myself but assuming you know how to do it just do this; create an invisible part at the point where the player clicked, then set the BodyPosition of the part you are trying to move to the placed parts position, and delete the part ace12345678135 50 — 8y
0
Thanks for the input but this is not quite what i need. jordan0810 55 — 8y

1 answer

Log in to vote
0
Answered by
cabbler 1942 Moderation Voter
8 years ago
Edited 8 years ago

You should use a BodyPosition, as that's pretty much exactly what it's for. And might not need a BodyGyro. Here's my attempt.

local player = game.Players.LocalPlayer
local UIS = game:GetService('UserInputService')
local mouse = player:GetMouse()

UIS.InputBegan:connect(function(input,gpe)
    if gpe then return end
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        --mouse click
        local char = player.Character
        if char and mouse.Target then
            local head = char.PrimaryPart
            if head then
                local p = mouse.Hit.p   

                local part = Instance.new('Part')
                part.CFrame = CFrame.new(head.CFrame.p + (p - head.CFrame.p).unit * 3 , p)

                local bodyp = Instance.new('BodyPosition',part)
                bodyp.Position = p
                bodyp.MaxForce = Vector3.new(1,1,1)*1e4
                bodyp.P = 1e4

                part.Parent=workspace       

                game:GetService('Debris'):AddItem(bodyp,10) 
                bodyp.ReachedTarget:connect(function()
                    bodyp:Destroy()
                end)

            end
        end
    end
end)
Ad

Answer this question