I have a gun that creates and shoots out an unanchored bullet (so it can trigger the touched event when it hits certain destructible blocks). However, unanchoring it just causes it to fall through the ground almost immediately once it's fired due to gravity, so how can I disable it just for this one part (nothing else)?
Here're my current configs for the bullet:
local bullet = Instance.new("Part") bullet.Name = "Bullet" bullet.Anchored = false bullet.CanCollide = false bullet.Massless = true bullet.Locked = true bullet.Parent = game.Workspace -- turn off gravity here
I couldn't find any tutorials on this, so any help would be appreciated :)
What you would need to do is apply a force to the part that is equal but opposite to the force of gravity. To do this you can use a BodyMover like BodyForce, or one of the newer physics constraints like VectorForce, and, using Newton's Second Law we can figure out the force we need to apply.
Newton's second law states that F = ma.
We need to find F, so we must multiply mass (m) by acceleration (a).
Mass can be found with the
Acceleration can be found as a property of workspace,
Therefore the force you must apply will have to be upwards, on the y axis, like so:
VectorForce.Force = Vector3.new(0, Part:GetMass()*workspace.Gravity, 0)
(Making sure that the VectorForce is set to apply the force in world space, not local space)
EDIT: An example, creating the attachment and VectorForce and applying the proper force
local attachment = Instance.new("Attachment", bullet) local vectorForce = Instance.new("VectorForce") vectorForce.Force = Vector3.new(0, bullet:GetMass() * workspace.Gravity, 0) vectorForce.ApplyAtCenterOfMass = true vectorForce.RelativeTo = Enum.ActuatorRelativeTo.World vectorForce.Attachment0 = attachment vectorForce.Parent = bullet