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

How do i make this part go towards another part with BodyVelocity?

I'm trying to make a magnetic part, that attracts certain things to it. I'm using BodyVelocity, but i'm not really sure how to go about attracting other parts to the magnet.

local l = prt:clone() ---this is the magnet, just imagine its in the workspace and all that stuff

for i, v in pairs(game.Workspace:GetChildren())do
	if v:findFirstChild("Humanoid")then
	if v:findFirstChild("Humanoid") ~= player.Character:findFirstChild("Humanoid")then
print(v)	
		if(l.Position-v.UpperTorso.Position).magnitude <=50 then
  local bv = Instance.new("BodyVelocity", v)
	bv.MaxForce = Vector3.new(1e8,1e8,1e8)
	bv.Velocity = l.lookVector * v:findFirstChild("RicoStacks").Value -------RicoStack is a NumberValue that i want the velocity to go by
end
end
end
end

1 answer

You can subtract the two vector3 points and use it to create a position vector between the two points. This will move one of your object toward the other.

(Object2.Position - Object1.Position)

This will create a vector relative to Object1's position, pointing toward Object2. In this case I believe this is what you're looking for.

bv.Velocity = (1.Position - v.UpperTorso.Position)

This should set the Velocity in the player's torso's vector to point toward the magnet. The speed the player will head toward the magnet is proportional to the magnitude of that vector. If you'd like to alter the magnitude you can use vector3.unit to grab the unit vector then scale it by your a chosen magnitude/speed. That would look something like this.

bv.Velocity = (1.Position - v.UpperTorso.Position).unit * Speed

There is a disadvantage to doing this. The character will continue to move in that direction until the velocity is deleted or set back to Vector3.new(0,0,0).

Consider using BodyPosition and setting it's Position to the Position of the magnet.

3 comments

0

nothing is happening when i used ur script

0

also what is bodyposition

0

BodyPosition moves a part toward a set position. Just set the BodyPosition's position equal to the magnet's position.

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