Raycast Result is different on Client and Server

I’ve noticed sometimes the normal and position for shapecasts and raycasts is slightly different between the server and client when doing the same raycast under the same conditions.

System Information: 11th Gen Intel(R) Core™ i7-1195G7 @ 2.90GHz, 16.0 GB, Intel(R) Iris(R) Xe Graphics

This is a small reproduction file showing how the normal is different when raycasting:

RaycastBug.rbxl (46.1 KB)

3 Likes

Not likely a bug. These numbers are within range for floating point inaccuracy to be a factor.

Do the same cast again on both and all 4 times should be different numbers. especially if you wait a sec in between

i personally dont think you should care that much about millionths of a precision point. just 3-4 decimals is enough. unless it disrupts your calculations (say you want vectors to be close to each other), you should try a Vector3:FuzzyEq() method.
its still a bit strange behavior it produces, but i believe its just inaccuracies

2 Likes

That’s wild. Have you tested two indentical raycasts on the client only? Are they different?

I’ve tried rounding to 1 decimal but it is still inaccurate in some extreme situations. I’m trying to do a deterministic physics simulation so it’s best if it was the same

It is still the same. This is doing task.wait() and I also tried doing task.wait(5)


I assume there is just different ways of calculating floating points based on my hardware and the server. But I think why rounding to decimals didn’t work for me is because when you create a vector it adds some extra precision

1 Like

I’m going to try rounding to the nearest multiple of .125 so there isn’t floating point error maybe

actually I’m not sure if this is the case because the vectors are the same when doing regular calculations it seems to be something about raycasts

forget what I said here. it does seem to just be a floating point issue with vectors in general. most likely I’ll just use the FuzzyEq method suggested to check if the state is close enough

This happens because CFrames are not replicated exactly to save bandwidth - We convert to quaternion and apply some compression.

The server has the exact CFrame, but the client has a slightly different compressed one. Raycasts themselves work the same between client and server.

You can see the real difference by replacing print(result.Position) with print(result.Instance.CFrame) in your repro.

2 Likes

Thanks! This helps a lot. I was able to rebuild the original CFrame from the components on the client and now everything is good.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.