How could I make a part face foward when on a part?

Hello, I am making a script that simply lets your character throw a C4 and let it stick to most objects. Though I’m trying to force the bomb to be launched at a trajectory so the bomb will always be facing forward no matter what it is on.

The code for it to be thrown:

local forceMultiplier = 50
C4:ApplyImpulse(Direction * forceMultiplier)

local touchedPart = false

while Run.Heartbeat:Wait() do
	for _, v in pairs(C4:GetTouchingParts()) do
		if not v:IsDescendantOf(Char) and not v:IsDescendantOf(C4) and not touchedPart then
			print("Touched", v)
			touchedPart = true
							
			C4.Parent = v
			local weld = Instance.new("WeldConstraint")
			weld.Part0 = v
			weld.Part1 = C4
			weld.Name = "C4Sticking"
			weld.Parent = C4

			C4:SetAttribute("Activated", true)
			C4.CanCollide = false
			end
		end
	end

I’m thinking about either changing the C1 or trying to change the trajectory itself but I’m not too good at both physics and math.

Any help is appreciated :grinning:

While the bomb is in the air:
while not C4:GetAttribute("Activated")do C4.CFrame = CFrame.lookAt(C4.Position, C4.Position + C4.AssemblyLinearVelocity.Unit, C4.CFrame.UpVector) end

This does not work. It will teleport the C4 to Y level -100000 instead of doing what its suppose to

That’s because the velocity is 0. add if C4.AssemblyLinearVelocity.Magnitude == 0 then C4.AssemblyLinearVelocity += Vector3.new(0, 10, 0) end.

Yeah this again does not work. Now it malforms the CFrame causing both the C4 and the part it sticks on to both be faced the same way.

That’s because the loop isn’t stopping when the C4 is done. It should only loop when it is flying in the air waiting to hit the target.

If I’m correct, while __ do breaks when the parameters of __ are not meet. So when the attribute changes to true, it should break?

Correct, but it must be synchronized correctly.

What do you mean when you say “synchronized”

Well it must be running correctly. The while loop must have task.wait() in it.

1 Like

I’m still not understanding why it changes the wall part CFrame?

Does me parenting the C4 to the object affect it’s parent Cframe as well?

Don’t do that. Weld the part 1 second after setting the attribute.

Why would I need to wait a second after the attribute changes?

we can see if the problem is the while loop or not.

None of the parts move when I delay it by 1.

So I did retest it without a delay and yes it is the loop.