Hi. I’m making a survival (Booga Booga Inspired) / civilization (Humankind 3 Inspired) game. I have not yet started the civilization part, but a lot of survival stuff are done. The only thing I’m not satisfied with my code for now is for the trees; I suck at physics. Assume that I am a 2 year old baby when it comes to CFrames, Vector3, and any physics-related property/instance. You must now think, I would use a pre-animation right? So then I spent the next 4 nights of my life without sleep, going to school with 0 braincells left, just to try and make a working tree falling system that scales on mass.
This is how I currently handle it, since I have NO idea how to make the physics work:
--Tree class (server-only)
Tree.Types = {
["Small"] = {
Health = 30;
Model = TreeAssets.SmallTree;
SpawnBoundaries = {
{x = -7.95, y = TreeAssets.SmallTree.PrimaryPart.Position.Y, z = -66.524};
{x = 59.343, y = TreeAssets.SmallTree.PrimaryPart.Position.Y, z = -177.606}
};
FallImpulseMultiplier = 2000
};
["Medium"] = {
Health = 60;
Model = TreeAssets.MediumTree;
SpawnBoundaries = {
{x = -7.95, y = TreeAssets.MediumTree.PrimaryPart.Position.Y, z = -66.524};
{x = 59.343, y = TreeAssets.MediumTree.PrimaryPart.Position.Y, z = -177.606}
};
FallImpulseMultiplier = 26000
};
["Large"] = {
Health = 100;
Model = TreeAssets.LargeTree;
SpawnBoundaries = {
{x = -7.95, y = TreeAssets.LargeTree.PrimaryPart.Position.Y, z = -66.524};
{x = 59.343, y = TreeAssets.LargeTree.PrimaryPart.Position.Y, z = -177.606}
};
FallImpulseMultiplier = 45000
};
["Immense"] = {
Health = 500;
Model = TreeAssets.ImmenseTree;
SpawnBoundaries = {
{x = -7.95, y = TreeAssets.ImmenseTree.PrimaryPart.Position.Y, z = -66.524};
{x = 59.343, y = TreeAssets.ImmenseTree.PrimaryPart.Position.Y, z = -177.606}
};
FallImpulseMultiplier = 4000000
};
}
function Tree:Destroy(player: Player?)
Identifier.erase(self)
local FallImpulseMultiplier = Tree.Types[self.SubType].FallImpulseMultiplier
local model: Model = self.Model
local aligner = model:FindFirstChild("TreeAligner")
local part = model:FindFirstChild("PrimaryPart")
local fallDetector = part:FindFirstChild("FallDetector")
part.Anchored = false
aligner.CanCollide = true
if player then
local character = player.Character
local root = character:FindFirstChild("HumanoidRootPart")
local impulseDirection = (part.Position - root.Position)
impulseDirection = Vector3.new(impulseDirection.X, 0, impulseDirection.Z)
if impulseDirection.Magnitude == 0 then
impulseDirection = Vector3.new(1,0,0)
end
impulseDirection = impulseDirection.Unit
local IMPULSE_POSITION_OFFSET = part.Size.Y * 0.9
local impulsePosition = Vector3.new(part.Position.X, part.Position.Y + IMPULSE_POSITION_OFFSET, part.Position.Z)
local impulse = impulseDirection * FallImpulseMultiplier
part:ApplyImpulseAtPosition(impulse, impulsePosition)
print(impulse)
else
print("Player not passed!")
local impulseOffset = part.Position.Y/1.9
local impulsePosition = Vector3.new(part.Position.X, (part.Position.Y+impulseOffset), part.Position.Z)
local impulse = Vector3.new(0, 1, 0) * FallImpulseMultiplier
part:ApplyImpulseAtPosition(impulse, impulsePosition)
end
local treeTouchedGround = false
local tempTouched
tempTouched = fallDetector.Touched:Connect(function(hit)
if treeTouchedGround then return end
if TagInPlural.HasTag(hit, "Ground") then
warn("Tree has touched ground. Deleting.")
Debris:AddItem(model, 5)
tempTouched:Disconnect()
treeTouchedGround = true
task.wait(.2)
for _, descendant in pairs(part:FindFirstChild("Leafes"):GetDescendants()) do
if descendant.Name == "PrimaryPart" and descendant:IsA("WeldConstraint") then
descendant:Destroy()
end
end
part.AssemblyAngularVelocity = Vector3.new(0,0,0)
end
end)
task.spawn(function()
for i = 1, 10 do
i += 1
task.wait(1)
end
if not treeTouchedGround then
warn("Tree got stuck somewhere and did not completely fall. Deleting.")
treeTouchedGround = true
tempTouched:Disconnect()
model:Destroy()
end
end)
ResourceService.SpawnedResources[self.BaseType]["Spawns"] -= 1
end
This is how I made it, manually adjusting the fall-multiplier per tree type, to make it fall the way I want it to fall. All I want to know is: how can I make this system automatically discover the impulse Vector3 necessary given the physical properties of my tree’s PrimaryPart? I want to apply the force on top of the tree in order to achieve the kind falling the trees do. You can change this method if it preserves the tree-like falling aspect.