ApplyImpulse() sends everything in one direction

I’ve got a bit of code that creates little gore bits when the player dies here:

local newGib = gibs[math.random(#gibs)]:Clone()
newGib.CFrame = CFrame.new(ray.Position) + Vector3.new(0, 4, 0)
newGib.Parent = game.Workspace.Map.Gore.Gibs
newGib:ApplyImpulse(Vector3.new(math.random(-400, 400), math.random(4, 20), math.random(-400, 400)))

The only really important line is the last one. Whenever I try to add velocity to the gibs, they go flying in one direction together, as seen here (Blood warning!):

Am I doing something wrong (Probably)? ApplyImpulse() has little to no documentation about it so my assumption is that it applies the force towards a position that I set. If there is a better way to do this please tell me :slight_smile:

6 Likes

Hi, a bit of a stupid question, but is the code part of a loop?

2 Likes

Yea. Its in a for loop:

			for i=0, amount do
				local newGib = gibs[math.random(#gibs)]:Clone()
				newGib.CFrame = CFrame.new(ray.Position) + Vector3.new(0, 4, 0)
				newGib.Parent = game.Workspace.Map.Gore.Gibs
				newGib:ApplyImpulse(Vector3.new(math.random(-400, 400), math.random(4, 20), math.random(-400, 400)))
			end
2 Likes

I don’t think it’ll do any difference, and it’ll probably make it less optimised, but would you mind doing this just to try it?

local tempgibs = {}

for i=0, amount do
    tempgibs[i] = gibs[math.random(#gibs)]:Clone()
    tempgibs[i].CFrame = CFrame.new(ray.Position) + Vector3.new(0, 4, 0)
    tempgibs[i].Parent = game.Workspace.Map.Gore.Gibs
end		

for _, gib in pairs(tempgibs) do
    gib:ApplyImpulse(Vector3.new(math.random(-400, 400), math.random(4, 20), math.random(-400, 400)))
end

Again, I doubt this will do anything, but it’s worth a shot. ApplyImpulse is a weird method, it has virtually nothing on it. If this doesn’t work I’d recommend trying VectorForce or another constraint like that.

1 Like

This gave the same result. I’m suspicious towards the vector3 argument. Anyways, Ill try using a VectorForce to move it and see how it goes since that has some documentation and ill show the result tomorrow.

ApplyImpulse applies a force that you input (which is a Vector3). It does not make your parts go towards that Vector3 as you suspect. It’s like setting .AssemblyLinearVelocity on a part. You aren’t setting an end position, you’re just setting a force.

1 Like

Try printing what your Vector3s are and see if their numbers actually differ.

If math.random() keeps returning the same result try setting seeds with math.randomseed(os.clock()).

1 Like

Print out the numbers that are being randomly generated, they might not be so random.

1 Like

I’ve changed the code so I can print out the velocities given here:

local newGib = gibs[math.random(#gibs)]:Clone()
newGib.CFrame = CFrame.new(ray.Position) + Vector3.new(0, 4, 0)
newGib.Parent = game.Workspace.Map.Gore.Gibs
local forceVector = Vector3.new(math.random(-400, 400), math.random(4, 20), math.random(-400, 400))
print(forceVector)
newGib:ApplyImpulse(forceVector)

The result is the same, All of them get flung in the same direction. This is what the output console looks like:
image

The vector that is inputted IS random. If you want to see the entire function that handles spawning gibs, tell me, since it spawns all of them at the same position. Ill try other people’s solutions in the meantime :slight_smile:

Hey, I took a look at the VectorForce constraint. I ideally don’t want to use it since I’d have to involve task.wait() in my code to create the effect of an impulse. Although this could work, I’d much prefer applying the force through code (only) and without using task.wait() rather than having to create a constraint since this code runs for every gib part.

1 Like

Im guessing they might have some initial velocity when cloned. Try setting velocity to zero first.

1 Like

I figured it out, and the answer was embarrassingly simple. Just put all the gibs into a collision group and make them no collide with eachother:

-- Register gibs to a collision group and make them no-collide
physicsSerevice:RegisterCollisionGroup("Gibs") 
physicsSerevice:CollisionGroupSetCollidable("Gibs", "Gibs", false)

...

local newGib = gibs[math.random(#gibs)]:Clone()
newGib.CollisionGroup = "Gibs"
newGib.CFrame = CFrame.new(ray.Position) + Vector3.new(0, 4, 0)
newGib.Parent = game.Workspace.Map.Gore.Gibs
local forceVector = Vector3.new(math.random(-10, 10), 4, math.random(-10, 10))
newGib:ApplyImpulse(forceVector)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.