How to apply force on part to go to specific direction

Hello there.

So, I have a coordinate, that is the parts location (some red cylinder) and it needs to go to a specific direction (a white dot, that is where the player clicks on) with velocity, it doesn’t have to be full reach, it just has to be near where the white dot goes. How could I do such thing?

I want to use basepart:ApplyForce() but I don’t know what vector3 to put in it.

robloxapp-20210814-2226236.wmv (1.5 MB)

Alright, hold on, let me share the video in a much better way.

3 Likes

Usually you can just do

target.Position - origin.Position

that didnt fix the problem. Here’s more detail put into it.

server script

event.OnServerEvent:Connect(function(player, datovalioso)	
parttool.Parent = workspace
parttool.frutilla.CFrame = player.Character.HumanoidRootPart.CFrame * CFrame.new(0, 2, -5) * CFrame.fromEulerAnglesXYZ(0, 0, 90)
parttool.Enabled = false
proxy.Enabled = false
wait(0.2)
parttool.frutilla:ApplyImpulse(parttool.frutilla.Position - datovalioso)
--local bodyvelocity = Instance.new("BodyForce")
--bodyvelocity.Force = Vector3.new(0, 1000000, 0)
print(datovalioso)
--bodyvelocity.Parent = script.Parent.frutilla
wait(2)
--bodyvelocity:Destroy()
proxy.Enabled = true

end)

local script

script.Parent.Activated:Connect(functio(action, typeofaction) --theres no typo here
	local positieni = getMouseHit()
	local ball = Instance.new("Part")
	ball.Parent = workspace
	ball.Shape = Enum.PartType.Ball
	ball.Size = Vector3.new(1, 1, 1)
ball.Anchored = true
	ball.Position = positieni
	
throw:Play()	
	throw.Stopped:Wait()
activated = true
event:FireServer(positieni)
throwfinal:Play()
wait(3)

end)

There is many ways to create projectiles, but here is one simple way you should be able to do it (I did not test this, but I believe it should work):

local projectile: BasePart = ... --projectile part
local startPosition: Vector3 = ... --could be the projectile position or a character limb
local targetPosition: Vector3 = ... --mouse hit position in the world, usually
local velocity = 10 --desired velocity in studs/sec

local lookDirection: Vector3 = CFrame.lookAt(startPosition, targetPosition).LookVector --unit vector of the direction towards the target
local velocityVector: Vector3 = lookDirection * velocity --create a velocity vector that has a direction

projectile:ApplyImpulse(velocityVector) --apply the velocity force instantly on the projectile

Edit: (correction) ApplyImpulse takes in a force, not a velocity, so this just means that you will need to increase the velocity variable value in my code to be higher if the projectile has more mass (heavier) and fails to shoot anywhere.

15 Likes

there it is

4 Likes

Now what I need is to know where the part it collided with will go at. If it hits someone, another player, the script would detect if it touched it, find the position of the cylinder, then the position of the player, then it needs to produce a new position of where the player would go at based on the position of the cylinder and the player.

So if the cylinder is below the player when touched, then the next position the player will go at is upward, if it is behind the player it will make the player go forwards. How would I find this third vector3 with these other 2?

Edit: I already solved this. Substraction is the difference of two pieces for a reason, so finding the difference between the two gets me the difference, then I have to separate the original piece with that difference I just got.

1 Like