CFrame problems with making part rotate on its axis using raycasting

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Imagine the part on top is the humanoid root part, and the part on the bottom is the particle that is being raycasted onto the surface. What I want is for the bottom part to have the same y-axis orientation as the humanoid root part whilst still tilting on the surface it was raycast on.

  1. What is the issue? Include screenshots / videos if possible!

The problem is I have no idea where to even start when using CFrames for this problem. So far I raycast down, and set the particle parts cframe to the a cframe of the position + the raycast normal. I also believe it would involve using either the :ToWorldSpace or :ToObjectSpace events.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have looked on dev forums, but have yet to see any specifically dealing with raycasting normals and toworldspace or toobjectspace. This is the current line of code I have, and it almost works, but sloped surfaces change the orientation of the particle part. Here is a video of the problem, I want it coming out of the back of the player like it is on the flat surface and the sloped surface to the left, but for some reason on the other sloped surface it comes out towards the side.

Here is the code I have implemented currently to get the CFrame that the particle part gets.

----- get the part orientated to the surface, facing straight up.
local newCFrame = CFrame.new(slideResult.Position, slideResult.Position + slideResult.Normal)
----- cframe to use in the final cframe :ToWorldSpace event
local angleCFrame = CFrame.Angles(0,0,math.rad(-character.HumanoidRootPart.Orientation.Y))
----- the final cframe. The angles cframe at the end is to rotate the particle by 90 degrees and get it facing the right way.
local finalCFrame = newCFrame:ToWorldSpace(angleCFrame) * CFrame.Angles(0,math.rad(90),0)

Any help would be appreciated as I absolutely suck when it comes to CFrames.

1 Like

Try this:

local Look = slideResult.Normal:Cross(character.HumanoidRootPart.CFrame.RightVector)
local finalCFrame = CFrame.lookAt(Vector3.zero, Look, slideResult.Normal) + slideResult.Position

This works by getting a vector that’s perpendicular to the surface’s normal and the right vector of the HumanoidRootPart using the Vector3:Cross() function, this means that the vector will be looking in the direction of the HumanoidRootPart while still tilting on the surface

2 Likes

it works like a charm! thank you so much, i gotta look more into how normals work haha

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