How can I make part applyImpulse correctly?

How can I get my part to move fling away from my spawner part no matter what way the spawner might be facing and no matter what size my flinging part will be model or not?

This is my current script. The issue is that my part is not moving away from the spawner part.

	local part = workspace.Part:Clone()
	part.Parent = workspace
	part.CFrame = workspace.Spawner.CFrame
	part.Anchored = false
	
	--part:ApplyImpulse(Vector3.new(0,math.random(100,250),0) * part.Mass) --MAKE PART FLY AWAY
	part:ApplyImpulse(Vector3.new(CFrame.Angles(0,math.rad(math.random(-45,45)),0) * game.Workspace.Spawner.CFrame.LookVector) * part.Mass) --MAKE PART FLY AWAY FROM SPAWNER
	game:GetService("Debris"):AddItem(part, 3)

My old script looked like this:

game.Workspace.Part.Position = game.Workspace.Spawner.Position
game.Workspace.Part.BodyVelocity.Velocity = CFrame.Angles(0,math.rad(math.random(-45,45)),0) * game.Workspace.Spawner.CFrame.LookVector * 10

I am still unsure of which direction you want the part to go in, but I assume you want the part to get away from the spawner regardless of where the spawner is positioned relative to the part. Please clarify me if I am mistaken.

If this is what you are trying to achieve, then I suggest you use the following format:

part:ApplyImpulse((spawnerPosition - partPosition).Unit * magnitude)

part - obviously the part you want to apply the impulse to
spawnerPosition - the position of the spawner
partPosition - the position of the part
magnitude - how hard you want to apply the impulse

Take the following demonstration as an example:

As you can see, the part moves away from the part labeled “Spawner” regardless of where the two parts are positioned. I did not demonstrate this in the video, but adjusting the magnitude to a higher value would increase the impulse, and the opposite would occur if the value was lowered.

The code from the video:

script.Parent:ApplyImpulse((script.Parent.CFrame.p - workspace.Spawner.CFrame.p).Unit * 100)

Hopefully, I answered your question. If not feel free to reach out, and I will reply back.

3 Likes