How to align a mesh instance with a raycast result's normal?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wowzzers

What is the correct way to align a newly created mesh instance, like a decal, with raycast result’s hit normal along the Z axis?

enter image description here

I’ve created this questions some time ago, but the solution doesn’t work now.
https://forum.godotengine.org/42265/how-to-align-an-instance-of-an-object-to-a-collision-normal

:bust_in_silhouette: Reply From: wowzzers

I’ve managed to get it working. You just have to zero out the other 2 bases.

Here’s the function:

func align_with_normal(trans : Transform, normal : Vector3):
	trans.basis.x = Vector3.ZERO
	trans.basis.y = Vector3.ZERO
	trans.basis.z = normal.normalized()
	return trans

trans for the transform of the mesh instance, normal is for the raycast hit result normal.

But if there’s a better way, please, answer.

You don’t even have to normalize the result normal, I guess.

wowzzers | 2020-02-13 14:21