How can I make the humanoid look at the face of a part?

Let’s say I’ve got a raycast firing from the humanoid’s torso like this

local ray = workspace:Raycast(torso.Position, torso.CFrame.lookVector * 5)

This is for a ledge climbing script I’ve been working on, I have everything figured out but I can’t seem to understand how I would make the humanoid face the wall the raycast hits
How could I make the humanoid look against the face of the part hit by the ray? I’ve been looking for solutions yet couldn’t find any.

Much appreciated.

2 Likes

For making it face it on the X and Z only you can do this:

CFrame.lookAt(HRP.Position,raycastResult.Position*Vector3.new(1,0,1)+Vector3.new(0,HRP.Position.Y+HRP.CFrame.LookVector.Y)

This code may have typos, I’m writing it from phone so I’m not sure.

This solution results in the player looking towards the raycast position itself, so if the player does the ledge climb at an angle, they won’t face the wall directly

As shown in this image, the player in this instance will look at the raycast position, but i want them to look directly at the wall

make the player’ lookvector something like CFrame.new(raycast.Position-Vector3.new(0,0,1), raycast.Position).LookVector

  1. Find the vector that faces (roughly) the same way as rayDirection. You can do this with the Dot product.
  2. Make the humanoid face the same way as the corresponding look vector that is found from step one.

So in the case from my illustration, you would want to make your character face the -LookVector’s direction.

1 Like

Hm, so I’d have to use rayDirection:Dot() with every vector and see which one matches up the most?

Actually, there is a better and easier approach. If you read through the documentation, you will find that you get the normal from a raycast’s intersection. You can make your character look in the negative direction of that normal.

Doesn’t work for me, i’ll just use your previous approach

The humanoid ends up facing a certain direction no matter where i initiate the script from

My bad, just set the HRP lookvector to the negative normal of the raycast, that should work
aw @Brickman808 said it first

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