Tamayaren
(Tamayaren)
February 13, 2021, 2:11pm
#1
Hello! I’ve been trying to figure out how to face a part to the surface that a ray has been hit.
To understand better, here’s a diagram of the desired outcome:
Not really good at advanced CFrames, I’ve tried using CFrame.lookAt() but I do not know what variables to put in.
I’ll be using this for actions like wall-jumping, and things a like.
Any help is appreciated!
1 Like
heII_ish
(heII_ish)
February 13, 2021, 2:13pm
#2
part.CFrame = CFrame.lookAt(part.Position, part.Position + hitNormal)
2 Likes
Here is another method, using CFrame rotation between two vectors in order to align a parts UpVector and the surface normal vector of the wall.
Use the advanced CFrame technique to rotate the parts UpVector to align/ face the sameway with the walls normal vector.
The function we will be needing
local function getRotationBetween(u, v, axis)
local dot, uxv = u:Dot(v), u:Cross(v)
if (dot < -0.99999) then return CFrame.fromAxisAngle(axis, math.pi) end
return CFrame.new(0, 0, 0, uxv.x, uxv.y, uxv.z, 1 + dot)
end
Using it:
if grounded == false and rrray and rrray.Instance ~= mine and rrray.Instance.CanCollide == true and rrr…
Ninja_Deer
(Kertopher)
February 13, 2021, 2:17pm
#4
You want to look at the RaycastResult.Normal
.
https://developer.roblox.com/en-us/api-reference/datatype/RaycastResult
It’s a unit vector that points away from whatever surface you hit.
Here’s a nice image visualizing normals.
You can use this normal vector and have the part rotate in the same direction. You can also invert it if you want the part to look the other way by just doing -normal
.
1 Like
Tamayaren
(Tamayaren)
February 13, 2021, 2:18pm
#5
Oh, I may have overlooked the problem, thank you! I feel stupid into thinking it was such a simple solution, haha…