Finding Vector3 Parallel to ground

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

What I’m trying to do is change transform.basis.z according to ground normal:

Image 1

As you can see in a flat ground the transform.basis.z is (0,0,1)

Now in a angled ground as you can see:

Image 2

I need to set the transform.basis.z to the approximate following value: (0, -0.534175, 0.845374). Well you obviously would answer me saying: just call transform.basis.z right? Well I can do that because this rotation you are seeing is inside the editor I rotated the rigidbody for taking screenshots only, also the transform.basis.z I’m using is another node :

enter image description here

:bust_in_silhouette: Reply From: Magso

I had the same problem. link

I copied what someone did on Unity answers. Godot doesn’t have the Quaternion.LookRotation method so I used look_at towards the normal plus an offset by the forward direction so it keeps it’s orientation.

var raycast_point = raycast.get_collision_point() + raycast.get_collision_normal()
var player_direction = global_transform.basis.xform(Vector3.FORWARD)
look_at(raycast_point + player_direction, Vector3.UP)
rotation_degrees.x += 45

this way of solving the problem requires that player scale Vector3(1,1,1), because its similar to one I found on KidsCanCode site! That one is limited to this scale!

nonunknown | 2021-02-15 19:35

How do you mean? Scale doesn’t have anything to do with this because it’s a case of looking from point A to point B.

Magso | 2021-02-15 20:12