How to place the character (crabs in the sand) perpendicular to the inclined surface?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tort
:warning: Old Version Published before Godot 3 was released.

How to rotation the character (crabs in the sand) perpendicular to the inclined surface?
enter image description here

:bust_in_silhouette: Reply From: Zylann

I never tried it, but I think you could do a raycast on the ground to get its normal, then assign the rotation by doing a look-at and specifying the up vector:

crab.look_at(target_pos, ground_normal)

Does not exceed.

var target_pos = Vector3(10, 10, 10)
var ground_normal = get_node('ray_down').get_collision_normal()
get_node('krab_arm').look_at(target_pos, ground_normal)

enter image description here

Tort | 2016-09-08 20:37

What do you mean by “does not exceed”? It doesn’t works?

Zylann | 2016-09-11 00:31

:bust_in_silhouette: Reply From: Tort

Thanks for the tip, Zylann.

It was possible to make as follows

	var transf_temp = Transform()
	var norm = get_node('ray_down').get_collision_normal()
	transf_temp = transf_temp.looking_at(norm, (self_pos - path[0]))
	transf_temp.basis = transf_temp.basis.rotated(Vector3(1, 0, 0), 1.57)
	self.set_rotation(transf_temp.basis.get_euler())

Next question. How to make a smooth transition into a new transformation?

You mean crabs orientate too quick when they encounter a sharp edge?

Well in this case you can perform multiple raycasts (maybe 3 in a triangle around the crab should be enough) and average them.

Another idea is to do only one raycast but using an interpolation over time to smooth its movement. It’s cheaper but leads to slightly different results.

Note: I think you should replace this 1.57 by PI/2.0, would make more sense :slight_smile:

Zylann | 2016-09-11 00:24

:bust_in_silhouette: Reply From: Tort

Stable

Пояснение. Вертикальная ось должна интерполироваться быстрее смежных осей, что бы персонаж не делал кувырок при поворотах.
Explanation. The vertical axis should be interpolated axes adjacent faster that character would not do a roll when cornering.

if path.size() > 0:
	var transf_temp = Transform()
	var norm = get_node('ray_down').get_collision_normal()
	transf_temp = transf_temp.looking_at(norm, (self_pos - path[0]))
	transf_temp.basis = transf_temp.basis.rotated(Vector3(1, 0, 0), PI/2)
	
	var rot = self.get_global_transform().basis
	
	rot[0] = rot[0].linear_interpolate(transf_temp.basis[0], 0.05)
	rot[1] = rot[1].linear_interpolate(transf_temp.basis[1], 0.1)
	rot[2] = rot[2].linear_interpolate(transf_temp.basis[2], 0.05)
	
	self.set_rotation(rot.get_euler())