I need help with RayCast and Orientation

hello I need help, what I want to do is make the player turn to where the face of a wall is looking but it seems that always in any face the player always ends up turning towards the same angle, I have been trying several things but he keeps looking towards the same angle

The RayCast

		local Origin = torso.CFrame.Position
		local Direction = torso.CFrame.LookVector*10
		
		local Params = RaycastParams.new()
		Params.CollisionGroup = ("Coverture")
		Params.FilterDescendantsInstances = {character}
		Params.FilterType = Enum.RaycastFilterType.Blacklist
		Params.IgnoreWater = true

		local Result = workspace:Raycast(Origin, Direction, Params)

Code of rotation

			hrp.CFrame = CFrame.new(hrp.CFrame.Position) * CFrame.Angles(0, Result.Normal.Y, 0)

The blue cube is the lenght / distance of the RayCast


When the blue cube touches the big cube the player turns to the cube face view, this is how it should work

But…
When I touch another face this happens

It would help to have more code, but there is a problem with how you’re setting the cframe.

CFrame.Angles takes 3 angles in radians, but you just gave it the y component of a directional vector. You can make the CFrame you probably want like this:

-- CFrame at the hrp looking in the direction of the surface normal
hrp.CFrame = CFrame.lootAt(hrp.Position, hrp.Position + Result.Normal)

Another potential problem with your code is that you have the ray come out of the front of the character, but then you have the character spin the opposite direction so the ray probably doesn’t get to the wall after spinning. There are multiple potential ways to solve this.

Edit:

Glad I could help!

As I was looking at your response I realized you probably only want the character to rotate on the y axis (not up and down too). If you want it to only rotate on the y axis, you can use this code instead:

-- CFrame at the hrp looking in the direction of the surface normal (No up-down/only rotating on y axis)
hrp.CFrame = CFrame.lootAt(hrp.Position, hrp.Position + Vector3.new(Result.Normal.X, 0, Result.Normal.Z))
1 Like

haha cool that worked for me, you made it look simple thank you very much

CFrame.lookAt
1 Like