How to get corrected orientation based off a raycastresult or part

I have a knockback system, where when you land on the ground, you stay there for a second then roll up. Now I’m trying to make it where if the knocked person hits a wall they get stuck on the wall, but I am having problems with having correct rotation for them. I am using ray casting, and I have tried things like math to get the degrees for the radian between the knocked player and the wall, or even the person who knocked them. I have tried many things and I am no longer sure what to do.

The code that I have tried using but not getting correct rotation after many edits:


I have done stuff with the degrees and if statements to change rotation, and used different types of rotation, but to no avail.

Have you tried casting 2 rays? This way you can use trigonometry across 3 points to calculate everything.

I have not, but I don’t see that method helping. I’m not sure how it would help to cast multiple rays when im simply having rotational issues, or maybe I’m just not seeing how it could help.

Actually, I need to correct what I said. Casting 3 parallel rays, which are very close to each other, would enable you to calculate the relative orientation. How? You can use the difference in raycast lengths to form triangles, and apply trigonometry to obtain the angles

1 Like

Here’s a 2 dimensional illustration representing 2 rays
image

1 Like

Now I see, but I’m not too sure how to apply that. I’m not great at doing advanced math, bare with me.

This may help you

I managed to fix it with a singular raycast, but what it took was multiple orientation checks for the character compared to the person who knocked them, every 22.5 of a rotation it would check and rotate accordingly, this was truly a pain but in the end was worth it. Thanks for trying to help by the way, but that article did not help.

1 Like

I figured out the math for this, and have it completely working, except… I’m repeatedly firing a wait() function until their knockback ends for the raycast, but that isn’t fast enough. If the timing is really bad then they will just hit the wall and the anti-fling kicks in, and the wall splat/wall slam fails. Is there any faster way to do loops on the server than wait()?

Game:GetService("RunService").RenderStepped:Connect(function()
or
.Heartbeat