Magnitude or raycast takes up more resources in this scenario?

So let’s say you have your average combat game. You detect when the punch tool is clicked through an event obviously, now for hit detection.

I wouldn’t use spatial queries as they hog more resources, a Touched event would have bad detection. So I’m between raycasting/magnitude. I believe magnitude would work the best as raycasting would take up more resources right?

Or am I wrong, not sure. Either way I think the difference would probably be very small, but please tell me.

1 Like

Depends on if you have to aim the ability or not

Magnitude (distance) checks could be more performance intensive when used in mass. I would suggest using spatial queries as they are more practical in this scenario, and they have more functionality.

1 Like

i did tests with 100 iterations running every .stepped and magnitudes tended to be fastest where

spatial queries tended to be around 1 ms, 1.1ms

raycasts 1 ms .09 ms

magnitudes .0055 ms sometimes upwards of .008

2 Likes

Magnitude is just calculation with the most expensive part is calculating square root. So it is not as intensive as raycasting and spatial queries, which both require a lot of config and moving part to factor in.

Still if you want to distance checking. I recommend using fast Distance checking algorithm which does not require calculating square root.

Maybe I should’ve been more specific, I was talking about like a regular punch tool for example.

With a regular punch tool, spatial queries are more than enough to use. You’re using it once per attack, you wont have any issues with it at all.

The only time you want to worry about them is if you are querying every frame, in which you can optimize or opt to a different mechanism.

But what would still be the most optimal? Even if it’s a slight difference.

for punch and close range abilities I use a combination of raycasts and magnitude checks. So raycast from the players mouse to the target they want to punch and then magnitude check if they are in distance on the client and then again on the server.

And with how many rigs are you querying per iteration?

1 Like

If they raycast it would be just 1

Im asking that because magnitude checks will always be more performant compared to raycasting or spatial querying, if there is only 1 rig being queried. There would realistically be more like 50 rigs, therefore making a more accurate benchmark.

But aren’t punch tools usually just about how close you are to the other player?? I’m not sure why you’d need to raycast to the player’s mouse and the target.

What if there’s more than 1 player beside you? How do you know which one the player wants to hit?

Well, if you make the distance for getting punched like 2 studs. I don’t think those issues would really arise. Since you’d be quite close to the player (that you want to hit), of course on the player’s screen they will feel like they’re miles away, however technically speaking it would work as intended. And I don’t think we can really fix that due to ping.

It’s easier to do it the way I described, because your method involves using a magnitude check on EVERY player in the game until you find one that is closest to you and apply damage. What if you want to punch player A, but player B is closer and it punches the wrong person?

Yeah I guess, but I mean I don’t think it really makes much of a difference to be honest so I think I know which one to do now. Although raycasting may be better to stop a player from getting punched through a wall.