Can someone explain attachments, vectors forces, and angular forces for me here?

I can’t find a more IN DEPTH video on this so I’m really confused right now. This code is from a video that when a gun is fired, it knocks back someone. I put some explanations in next to lines I THINK I understand, so if you want to correct me on any of them, please do. Also, the code works but I would like someone to explain more what’s happening here and HOW it works, specifically the knockback function. Thanks!

local re = script.Parent:WaitForChild("RemoteEvent")
local damage = 20
local bang = script.Parent.pistol.GunShot

--Velocity and angular force
local function knockback(plrChar, enemyChar)
	if plrChar and enemyChar then
		local pHumRoot = plrChar:FindFirstChild("HumanoidRootPart")
		local eHumRoot = enemyChar:FindFirstChild("HumanoidRootPart")
		if plrChar and enemyChar then
			
			local dir = (eHumRoot.Position - pHumRoot.Position).Unit --Finds direction
			local attachment = Instance.new("Attachment", eHumRoot)--Puts attachment in enemy's humanoid root part
			local force = Instance.new("VectorForce", enemyChar)--Puts Vector force in enemy's character
			
		1.	force.Attachment0 = attachment -- Attaches Vector force to the humanoid root part
		2.	force.Force = (dir + Vector3.new(0, 1, 0)).Unit * 10000 -- where to force it to
		3.	force.RelativeTo = Enum.ActuatorRelativeTo.World
			
			enemyChar.Humanoid.PlatformStand = true
			
			local rotate = Instance.new("AngularVelocity", eHumRoot)--Places angular velocity instance in enemy's humanoid root part
		1.	rotate.Attachment0 = attachment --makes angular velocity attached to its own parent
		4.	rotate.AngularVelocity = Vector3.new(1, 1, 1) * 30
		5.	rotate.MaxTorque = math.huge
		3.	rotate.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
			
			
			game.Debris:AddItem(force, .2)
			game.Debris:AddItem(rotate, .2)
			game.Debris:AddItem(attachment, .2)
			wait(2)
			enemyChar.Humanoid.PlatformStand = false
		end
	end
end

local function onShoot(player, target)
	bang:Play()
	if target and target.Parent then
		local hum =  target.Parent:FindFirstChild("Humanoid")
		if hum then
			hum:TakeDamage(damage)
			knockback(player.Character, target.Parent)
		end
	end
end

re.OnServerEvent:Connect(onShoot)

Key:
1.Why do the forces(Vector and Angular) attach to their own parent? Is the object just put there just so that it has a parent, or is it there to affect the attachment?
2.I understand this lines because the force goes in that direction. But is the .Unit part necessary? And why does it multiply by 10000?
3.I just don’t understand these
4. What exactly do the numbers in Vector3.new do? And why is it multiplied by 30???
5. I’ve been told this is just always math.huge, but I’ve also been told that it does matter, but how, and what does it do?

I understand the meaning of vector. But what does Angular force do? I tested out what happens without angular force, and it just makes the shot player go flying in pretty much a straight line. I thought it was probably just the 10000, but when I added angular force, it seemed like the strength of the vector go weaker.

Forces only work while inside of workspace, so they’re commonly parented to their attachment or some other thing close by for organization purposes. Otherwise, the parent has no effect on the forces.

.Unit is used to get a new direction which faces upwards more, while still having a length/magnitude of 1. But since a vector with the magnitude of 1 is pretty weak for a force to push a character, it’s multiplied by a large number, making the force stronger while maintaining the same direction.

(If .Unit wasn’t used, then one thing which would be possible would be to have a direction vector of (0, 1, 0) which would equal to (0, 2, 0) when added to itself, causing the knockback force to be twice as strong as normal since its magnitude is 2. By using .Unit, it ensures that all knockback forces are of equal strength.)

The RelativeTo property dictates what frame of reference is used to apply a force. It can be either relative to an attachment or the world.

For example, pretend there is an attachment which is rotated to face downwards, and a force under that attachment with a force vector like (0, 5, 0). If the force was applied relative to the world, then the attachment would move upwards in the Y direction as the force vector points that way in the world’s coordinates. But if the force was applied relative to the attachment (which remember, is facing downwards), then the attachment would move sideways in the world because the force is pointing towards the top of the attachment, which is pointed sideways.

The attachment has its own frame of reference based on its rotation, where what it sees as forwards is downwards in the actual world, and what it sees as upwards is sideways in the actual world.

The angular force is probably there to make the character spin as it gets knocked back. I don’t know how it might affect the strength of the knockback, it could be causing a physics quirk where rotation affects flying trajectory. I don’t know for sure though.

Torque is the rotational equivalent of force, essentially. Sometimes, you want to put in more force to ensure a force applier works properly, which is why you use math.huge as it’s basically equal to infinity in Roblox.

Sorry if any of these explanations are confusing. If anyone reading this sees I am wrong somewhere, feel free to correct me.

2 Likes

Thanks! All your explanations have made complete sense to me. Just a couple questions. So the * 30 is basically the same as 10000 but for spinning? Why can’t it be * 10000 also?

So if I’m understanding this correctly, the it just makes a direction, and then times it by 10000 to make it stronger? Why can’t I put (0, 10000, 0)? And what do you mean it ensures all knockback forces to be equal of length?

Blockquote
But since a vector with the magnitude of 1 is pretty weak for a force to push a character, it’s multiplied by a large number, making the force stronger while maintaining the same direction.

(If .Unit wasn’t used, then one thing which would be possible would be to have a direction vector of (0, 1, 0) which would equal to (0, 2, 0) when added to itself, causing the knockback force to be twice as strong as normal since its magnitude is 2. By using .Unit, it ensures that all knockback forces are of equal strength.)

Since the angular velocity is a vector, multiplying it will do the same thing as multiplying any other vector, making it equal to (30, 30, 30). It’s not multiplied by 10000 because then the angular forces would try rotating the character at 10000 radians per second around each axis, which would be a high number of revolutions in one second. I don’t know the original writer’s intent with the rotation, though, but I assume they chose 30 to give the character a reasonable spinning speed during knockback.

I’ve made a diagram to visualize the changes being made to the vector:

In the case of adding knockback, the original writer intended to make the knockback force go somewhat upwards compared to the actual direction. However, by adding the (0, 1, 0) vector, it changes the length as well. That’s why .Unit was used, so that the vector is returned to length 1. If it wasn’t used, the resulting force would be stronger than other direction vectors which point more downwards, which wouldn’t make sense.

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