I was wondering what would be the best way to detect if a player hits a wall at a certain speed?
In my game I wanna make it so if a player gets launched into a wall they take damage. Obviously, I wouldn’t want to damage them if they just simply touched the wall so how should I go about detecting their speed to make sure they are going fast enough to take damage from hitting the wall?
I played around on a baseplate a little bit and implemented the concept of instantaneous speed. Basically, speed at a particular instant of time, where the time interval is infinitesimally small. I used the .Heartbeat
function of RunService to get the delta time value, and used it for the calculation.
Here’s the code:
local playersService = game:GetService("Players")
local heartbeat = game:GetService("RunService").Heartbeat
local part = script.Parent
local debounces = {}
part.Touched:Connect(function(hit)
local player = playersService:GetPlayerFromCharacter(hit.Parent)
if not player then return end
if debounces[player] then return end
local char = player.Character
local hrp = char.PrimaryPart
if not char or not hrp then return end
debounces[player] = true
local pos = hrp.Position
local dt = heartbeat:Wait()
local dS = (hrp.Position - pos).Magnitude / dt
print("Instantaneous speed of", player, "was", dS)
task.delay(1, function()
debounces[player] = nil
end)
end)
Place file:
Instantaneous Speed.rbxl (32.5 KB)
This doesn’t seem like an efficient way to go about it. Putting a touched event on EVERY part in the game wouldn’t be too performance friendly especially taking into consideration my game is open world with a HUGE map and lots of parts.
well you could put the player into a flung state if they are being flung and then calculate velocity after they touch something depending on the velocity deal damage or you could use suvats etc really there are a lot of options i personally being lazy would just calculate the velocity after being in flung state and then touching something by obviously the magnitude of the distnace divided by time
what is “flung state”?
and if possible could you elaborate on what ur trying to say I dont quite understand.
are you telling me to do something similar to what 0V_ex is saying but instead using humanoidrootpart.touched?
i havent read vex’s but basically im assuming there is going to be some sort of punch to the player that makes them fling, when that punch happens set a variable called “Flinging” to true you can do this by adding an additional attribute in the humanoid or anywhere on the player or the players character or a local script or something also log the position of the character, at increment time periods while being flung )flinging = true) log the distance travelled end the loop on the next collision and deal the damage determined by the overall distance travelled divided by the time
Thank you for clarifying!
(aaaaaaaa character limit)
okay so theres one issue with your concept.
I am going to be having players flinging in many directions
for example.
lets say I have a move where I punch an opponent and they go flying.
just them dragging across the floor will count as a hit and I dont want that.
I also have moves where you lift people into the air and slam them into the ground. While falling I wouldn’t want them to take dmg just by skimming against a wall. How can i solve this?
What I’d do (which honestly isn’t any better in terms of performance) but on heartbeat when your character goes over a speed threshold, cast a ray in the direction they’re going and if you detect an impact, check the magnitude until it becomes extremely low, or check change in player speed.
Even then you could just check change in velocity, like if someone goes 80 to 0 in a split second then obviously they hit something.