Hi, everyone.
How can I use a BodyForce or some type of force to send an object directly towards a coordinate without any interference such as gravity.
The most common method is to get a LookVector by making a CFrame from the point you wish to start and the point you wish to go to. For example:
--Let's assume P1 and P2 are 2 positions that are already assigned.
--P1 is the starting point and P2 is the destination
local Speed = 50 --or however fast you want
local LV = CFrame.new(P1,P2).LookVector*Speed
LV is now a Vector3 that is pointing from where you wanna start to your desired point, and thus you can use a BodyMover such as BodyVelocity to make it move the way you want.
You could use an AlignPosition: AlignPosition | Documentation - Roblox Creator Hub
To make the part ignore gravity, just set its Massless property to true.
Well if you want to use an a force object such as vector force then you’ll need to understand a couple of underlying principles regarding physics.
The first is that acceleration is the rate of change of velocity. This means that if we subtract our target velocity from our actual velocity we’ll be left with the acceleration we’d need to travel at to reach our target: targetVel - object.Velocity = acceleration
The second is newton’s second law which states that force = mass * acceleration
. We’ll use this for two things.
- Calculate the force needed to reach our target
- Offset gravity so it’s not offsetting our target reaching force
Using these two things in combination we can update our force to move our object to a target.
local part = game.Workspace.Part
local target = game.Workspace.Target
local vForce = part.VectorForce
vForce.RelativeTo = Enum.ActuatorRelativeTo.World
game:GetService("RunService").Heartbeat:Connect(function(dt)
local targetVel = target.Position - part.Position
local acceleration = targetVel - part.Velocity
vForce.Force = part:GetMass() * (acceleration + Vector3.new(0, game.Workspace.Gravity, 0))
end)
Sure enough we have our result: