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

Applying force equation onto a Vector3 ?

Asked by 9 years ago

So today I was working on making a simple function that can apply the correct amount of force onto a cart I made and I had no errors with it when I tested my code, there is a BodyForce object within the car model that has its value changed according to the function and I don't understand why it isn't working correctly.

Force equation: Force = Mass * Acceleration

Also, I have my car within a model, and all the parts within that models are normal parts (No unions involved.)

Here is my code:


-- Variables local Acceleration = Vector3.new(4.905, 0, 0) local BodyForce = script.Parent.BodyForce local Model = game.Workspace.Car local Mass = 0 --Get mass for _, component in pairs (Model:GetChildren()) do if component:IsA("BasePart") then Mass = Mass + component:GetMass() end end --Apply it to the BodyForce BodyForce.force = Acceleration * Mass
0
Any errors in output? funyun 958 — 9y
0
Nope, I don't think it calculated it correctly. I don't know what values to insert for Acceleration to make it move by 1 stud AncientEdits 30 — 9y
0
Acceleration is distance per second per second, or m/2^2... I don't understand how you're trying to apply it to a vector3. HungryJaffer 1246 — 9y

1 answer

Log in to vote
0
Answered by
funyun 958 Moderation Voter
9 years ago

Functions don't actually run unless they're called upon.

function add5(x) --This initially won't run, it's just the definition of the function.
    return x + 5
end

print(add5(7)) --Here we're actually calling the function and printing what it returns.

function spam(iterations) --This won't run, it's the definition.
    for i = 1, iterations do
        print("spam")
    end
end

spam(10) --Here, we're calling the function. This will make the function run.

By the looks of it, you don't actually need functions in your script.

-- Variables
local Model = nil --Define "Model" here.
local Acceleration = Vector3.new(0, 0, 0) --Set to what you'd like.
local BodyForce = script.Parent.BodyForce
--Also, it's best that you set maxForce at infinity, if you haven't already.
BodyForce.maxForce = Vector3.new(math.huge, math.huge, math.huge)
local Mass = 0

--Get mass
for _, component in pairs (Model:GetChildren()) do
    if component:IsA("BasePart") then
        Mass = Mass + component:GetMass()
    end
end

--Apply it to the BodyForce
BodyForce.force = Acceleration * Mass
0
The code did not work correctly. AncientEdits 30 — 9y
0
I will edit my post with the updated code AncientEdits 30 — 9y
Ad

Answer this question