How to reflect rays on hit

I encountered something abnormal (Hopefully I’m not extremely wrong here)

Correct me if I’m wrong, but I’m very certain that the angle should stay the same despite the position of the part.

Not complaining though, this post is very helpful.

2 Likes

That is quite interesting. The only reason I can think of is that the normal vector being returned from the surface is slightly inaccurate depending on the position. Idk, would be interesting to investigate that futher.

3 Likes

Yeah keep on checking on that. Because this would most likely be a Roblox issue otherwise. The math being used for ray reflections that I use is about as accurate as it will get.

Just wanna know what is that? d times n or idk because d * n is multiply in script
Please answer

The centered dot is the symbol for dot product. You can find this throughout the code in the thread as well as the link from which the equation was sourced from. In a script, you would be using the Dot method of Vector3 to get the dot product.

3 Likes

Hey. I recently started programming so excuse my bad knowledge.
I used your “original” reflect ray script with the now deprecated ray.New() function.

(And I plan on using it, as it has functions/returns things that the new one doesn’t)

On that occasion, I was maybe wondering if you could share the original/deprecated version of the script? It will make it easier for us who already use parts of the original script (and don’t have the full script) to use the reflection.

Thank you @sleitnick !

Hi @sleitnick.
If I replicate the shooting effects to all the player clients (so not on the server) would it be bad for performance or? Thanks.

TL:DR; is it ok to use performance wise?

Should be ok to do that. Reflecting a vector is a very simple operation that doesn’t cost a lot. The costly part is raycasting itself. If you’re going to reflect a bunch of rays, I recommend adding a max number of reflections allowed.

1 Like

Hey, thanks for your answer @sleitnick!

Actually I do not use the reflection part (no offense lol :wink: ) so you think I should be fine, also without setting a max limit? Thanks dude :smile:

i have a question i am trying to do this but with a bodyvelocity, so what i need to change it to work?

How would I make pong physics with this?

How can i do this with bodyVelocity?

1 Like

Just get the origin and hit position and make it into a unit and multiply by how much speed you want.

local speed = 100
(ray.Position-origin).Unit*speed

Thanks so much! But the things is, I don’t want the raycasting code to run the whole time that the laser is shooting. I only want it to run when it hit something and make it bounce. Thanks!

That is kind of impossible because you will need to do something else other than raycast to dectect before it hits.

how can i use touch event to connect to a function that make the part ricochet? thanks!

1 Like

You dont have to run it constantly just run it once then tween the laser to the position it hits then repeat till it hits nothing tho you’ll have to calculate how long the tween should take so the speed would be consistent i also recommend avoiding things like BodyVelocity cuz its expensive for the server to do physics and if you were to trust players to handle physics how could you know someone isnt gonna exploit and just teleport the lasers they shoot right into other ppl? just stick to tweens and anchored parts.

If you want high player counts per server you shouldnt even be doing anything like moving things around in server let alone handling physics calculations as the replication tends to ruin latency a lot instead you’d tell players what got shot from where towards where etc. via remotes and create the laser locally and handle hit checks via rays at server but things like that only starts to become a real problem if you’re aiming for 30+ players per server etc.

1 Like

ok then thanks bro btw i set my server for only 18 plr max and also i guess i wont use body vel cuz i also found it’s laggy

1 Like

Hey, I have a problem here im using your script. When the bullet shoots out the speed is normal. Then, when it hits an object, the speed suddenly turned super slow. I tried a few more times and found out that the material the bullet/laser hits effects the speed of the bullet/laser. Can you help me out pls?

I think the problem might be because of the Step(stepDistance - (pos - oldPos).Magnitude) part. Maybe you should remove it? Idk since im not very good at raycasting and u know better pls help fix it thanks