I’m wondering what would be better for detecting when two characters are touching each other or colliding for purposes of tackling and blocking for an American football game.
I’ve been looking into Region3, Magnitude, Touched, and Raycast but I’m not quite sure which method would be the most inexpensive and efficient for what I’m needing.
The best pick out of those options, that uses the least amount of resources, is by far .Touched. .Touched is a event, meaning you don’t have to run it in a fast loop to continuously perform checks. You can easily listen for the .Touched event to fire, and then process the hit to see if it is what you want.
If you don’t want to use .Touched, Raycasting is your next best bet in my opinion. I would choose Raycasting over Region3s, because they use way less processing power, and can work just as consistently.
This is a great resource that uses Raycasting to detect collisions:
Magnitude is a great option for a lot of things, as it is just math and is very efficient. However, I don’t how well it would prove at hit detection. You would need to get the distances between one object, and all other objects in the game, to check for hits. This would not be a efficient idea.
There are many methods in the field of hit detection, but I hope you can find one that works great for your project!
I agree with @ExcessEnergy on .Touched being your best option.
When it comes to using .Magnitude for detecting characters touching each other, I’d also recommend against it. It would be having to be in a loop, constantly detecting the distances between all players in the game; having to run it’s formula that many times would eat away at resources little by little. They say one of the biggest things that eat away at resources is how .Magnitude takes the square root in it’s formula. Take a look at the makeshift .Magnitude function I created here (not for Roblox use): How does magnitude and range work? - #6 by MJTFreeTime
So your best bet would be using Humanoid.Touched, and checking to see if the part touched is part of a player. Something like this (in a script placed in StarterCharacterScripts):
local myCharacter = script.Parent
local myHumanoid = character:WaitForChild("Humanoid")
local function onCharacterTouched(hit, limbTouched)
if hit.Parent:IsA("Model") and hit.Parent:FindFirstChild("Humanoid") then
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
print("Hit by " .. player.Name .. "!")
-- Whatever you want to do here
end
end
end
myHumanoid.Touched:Connect(onCharacterTouched)