I’m trying to build an aircraft, and I want to make the vehicle take damage from dangerous contact with other vehicles and environmental objects. The problem is that I’m very inexperienced and have no idea where to even start with that kind of functionality.
Where do I start? Which functions in the API should I be looking at?
Weld all of the parts of the plane together. Then add a simple script that detects if the plane touches something. Here is an example:
local Plane = script.Parent
Plane.Touched:Connect(function(part)
if part:IsA("BasePart") then
for _i in pairs(Plane:FindFirstChild("Weld")) do
i:Destroy()
end
end
end)
Btw, this script might be wrong. Let me know if it is.
In @Discgolftaco231’s method you’d have to be sure not to use a Weld Script to weld the Parts together since those scripts just weld each Part with the next one it finds. The issue with that is that if you have the left wingtip welded to the right main landing gear then when your plane hits a hangar with the left wingtip then the landing gear and all Parts that were welded after it will break off your plane.
I wouldn’t really go with the touched thing, it would destory even if the place is sligthly touched. This type of system is dependent on your existing code. If your using OOP, and giving each plane a velocity property then use a region3 hitbox which moves with the plane, and if the region3 detects a part and the planes velocity is higher then an x amount then break the welds. if your not using welds then just unanchor the parts.
(Also you inspired me to make a plane system im going to start making one right now)
Unfortunately, this solution would not be viable - the plane has a variable speed and uses a weld script for its structure, so a solution this simple wouldn’t work as-written. I appreciate your response, though!
This might be a workable solution, but do you know how expensive of an operation .magnitude is? It seems like it might be more expensive than is strictly needed, but I can’t say for sure.
.magnitude isnt an expensive operation. It is just getting a property from a part. I mean what kind of plane crash game. Could you send pics, I might be able to give you a code sample if i know what style it is
while true do
if (workspace.Baseplate.Position - workspace.Part2.Position).Magnitude > 5 then
---
end
wait()
end
I mean we are creating a vector object each iteration (that’s not expensive) what’s expensive is lua making the magnitude property for each vector created, since the distance formula uses square roots. It’s not gonna lag your game or anything though. Also magnitude isn’t a property of a part to my knowledge, It’s a property of vector3.
Also plain mangitude checks don’t really have precice hitboxes. That’s the reason I usually use region3 or raycasting. But most likely magnitude checks are cheaper then these options, not sure though.
How would I use Region3 to detect collisions? I know they can detect objects in their boxes, but if I wanted usable fidelity it seems like I would need to have many of these and that seems like it might not be an ideal solution.
I’m not really sure what your saying, but if your asking wrapping a region3 around every single part that’s defintly bad. What im saying is making a region3 around the whole plane which represents the hitbox using some kinda center point in the plane.
The reason I don’t rely on magnitude checks is they don’t account for size. Even if the plane was touching something far away because it’s size is big it won’t account for collision. Since the distance is still far away.
Yah, and that happens with every method theres no away to get 100% precice collisions without doing a collison around every polygon which is SUPER expensive. With magnitude checks this gap would be even more because it doesn’t account for size. The most precice way would probably be the touched event around a part which is shaped like the plaine.
Touched events aren’t really reliable and they don’t work if your cframing a part to my knowledge.
Raycasting is probably more performant then region3, here is a module which can help you.
Would it be feasible to have say, 5 - 10 Region3 collision detectors? Because one single box wouldn’t be enough for this job but for this particular plane I would estimate that about six would do the job.
Yah it woudln’t be that bad, however if you want a lot of detectors raycasting would be a better option, but raycasting isn’t that good as acting as a precice hitbox.