Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Disable Gravity for a Specific Part?

Asked by 3 years ago

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 :)

1 answer

Log in to vote
4
Answered by 3 years ago
Edited 3 years ago

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 Part:GetMass() function.

Acceleration can be found as a property of workspace, workspace.Gravity.

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
0
Thanks -- this should work (I tried it with some other parts) but when I tried to put it in, it just showed me an error. Reminder that the bullet is massless and created as a new part(doesn't exist in the workspace when editing). Can you give me an example on how to do it by placing it the script I gave in the question? Bob4koolest 78 — 3y
0
I've edited my answer. Even though the bullet is massless, :GetMass() won't be 0, it will still have a mass. I'm not sure what parts of the physics engine Massless effects, but constraints aren't affected by it in this way, so this will still work. cowsoncows 951 — 3y
Ad

Answer this question