Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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.

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.

2 comments

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

0

Thanks for the input but this is not quite what i need.

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.