[Python] Aligning to Edges

I want to know how to align objects to the orientation of another object’s face’s normal. I’ve been trying to use the “alignToVector” But I get weird results and it lines up to world orientation of the normal. Or at least I think that’s what happens.

Sometimes I get the correct results, but only when I align to the normal in a certain direction.

I’m still working on a good ledge hang, how do I get my character to align to the edges?

Its interesting that you have errors, it should be a pretty straight forward cast and align, assuming your character faces y+:

  1. Cast a ray from the player along its local y+ axis
  2. If hit, align y axis to the negative of the hit normal

I’ve attached an example, is this what you mean?

Attachments

align_to_wall.blend (345 KB)

That’s exactly what I was doing… But I don’t understand, why do you align it to the negative hit normal? I thought a normal was just a perpendicular vector of a face. Aligning to the vector by itself should have worked in my mind, so I never tried using the negative hit normal. Obviously it works now, but I’m still confused. Thanks Andrew, very much appreciated!

Another question:
How can I just copy the Z value of an object’s orientation?

Edit:
So far what I did was this, using MathUtils:


ori = #Orientation of the object

ori_euler = ori.to_euler()
ori_euler[0] = 0
ori_euler[1] = 0
new_ori = ori_euler.to_matrix()

own.orientation = new_ori


Change the orientation matrix to an euler, changing the X and Y values to 0. Converting back to the matrix, then copying it.

Is there a better way? This works fine, as far as I know though.

"Is there a better way? "

yes :wink:

copyZ=other.orientation.to_euler().z

ori=own.orientation.to_euler()
ori.z=copyZ

own.orientation=ori

The normal of the plane points outwards from the plane. If you align your y+ axis (the axis your player faces) to the normal, then your player faces away from the plane. Aligning it to the negative of the normal makes you player face inwards, towards the plane.