So I want to make a flying broom like in harry potter. So I already made one and it works very well, there is just one bug and it is that the broom can fly through objects. That is because I am always changing the CFrame and that is the reason why it can’t go through objects.
I tried making a flying broom with alignrotation and vectorforce, but the vectorforce behaves really weird. My broom doesn’t fly up, even if I put a high force, it still doesn’t fly up and it gets really fast. If you know a better solution than using vectorforce, please tell me.
The force of gravity is probably negating any upwards force applied by the VectorForce. The default gravity is ~192 studs/s^2, and if you were aiming straight up giving a force of (0, 2000, 0) then that would be completely negated by a mass of just 10.5 mass units.
The best solution is probably to take gravity completely out of the picture while the broom is supposed to be in control. There’s no built in way to do this for just one player a time, but you can accomplish it by applying an upwards force that completely negates gravity. Something like this should work:
function disableModelGravity(model)
for _, part in ipairs(getModelParts(model)) do
disablePartGravity(part)
end
end
function enableModelGravity(model)
for _, part in ipairs(getModelParts(model)) do
enablePartGravity(part)
end
end
function disablePartGravity(part)
enablePartGravity(part)
local force = Instance.new("BodyForce")
force.Force = Vector3.yAxis * part:GetMass() * workspace.Gravity
force.Name = "AntiGravityForce"
force.Parent = part
end
function enablePartGravity(part)
local force = part:FindFirstChild("AntiGravityForce")
if force then
force:Destroy()
end
end
function getModelParts(model)
local parts = {}
for _, d in ipairs(model:GetDescendants()) do
if d:IsA("BasePart") then
table.insert(parts, d)
end
end
return parts
end
Just call disablePartGravity on the broom and character when the player gets on the broom, and enablePartGravity when they get off the broom.
You could simply set the AlignPosition's .Mode to Enum.PositionAlignmentMode.OneAttachment, .Attachment0 to an attachment inside of the broom part and set the .Position to the position of the CFrame you use for the broom that uses CFrame.
The anti-gravity did not work, because well first of all the player still has mass and the broom. You have to apply both with anti-gravity or apply anti-gravity on one and make the other one Masless.
So what I suggest doing is getting the mass of the character and broom (or make the broom Masless and then exclude it) then followed by using the anti-gravity force.
Oh yeah another note, try setting all parts of the player to Masless, same for the broom.
for _, descendant in pairs(character:GetChildren()) do
if descendant:IsA("BasePart") then
descendant.Massless = true
end
end
Then that’s the solution, as I said before in the original post
You have to apply the anti-gravity force on the broom and the character. Though in my opinion I do not really like the script @ThanksRoBama, because it uses quite a lot of functions. Instead I would personally use something like this:
local function toggleModelGravity (model, state)
-- if state is on then anti-grav gets turned on
for _, descendant in pairs(model:GetDescendants()) do
-- loop through all descendants of the model
if descendant:IsA("BasePart") then
-- verify that it is a part
local force = descendant:FindFirstChild("AntiGravityForce") or Instance.new("BodyForce")
-- get the previous AntiGravForce, or make a new one
if state then
-- set properties
force.Force = Vector3.yAxis * descendant:GetMass() * workspace.Gravity
force.Name = "AntiGravityForce"
force.Parent = descendant
-- else destroy
else force:Destroy() end
end
end