How to use body forces and body gyro correctly?
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.
01 | local UIS = game:GetService( "UserInputService" ) |
02 | local plr = game.Players.LocalPlayer |
03 | local mouse = plr:GetMouse() |
06 | local selectedUnit = nil |
08 | UIS.InputBegan:connect( function (input) |
09 | if input.UserInputType = = Enum.UserInputType.MouseButton 1 then |
11 | if mouse.Target.Name = = "Unit" then |
12 | selectedUnit = mouse.Target |
17 | print (selectedUnit:GetChildren()) |
18 | selectedUnit.BodyGyro.CFrame = CFrame.new(selectedUnit.Position*Vector 3. new( 1 , 0 , 1 ), mouse.Hit.p*Vector 3. new( 1 , 0 , 1 )) |
19 | selectedUnit.BodyVelocity.Velocity = CFrame.new(selectedUnit.Position, mouse.Hit.p).lookVector * speed |
20 | repeat wait( 0.5 ) until (selectedUnit.Position - mouse.Hit.p).magnitude < 3 |
21 | selectedUnit.BodyVelocity.Velocity = Vector 3. new( 0 , 0 , 0 ) |
Any suggestions, improvements, tips or even a complete re-haul of what i'm attempting would be appreciated.