Hello

I just recently started with game development and godot. I am trying to create an rts game. I watched a tutorial about camera and unit movement. It works but for some reason the units float above the ground as soon as they first move. https://imgur.com/gUkMOaI

Does anyone have an idea what is going wrong? Thanks in advance to those who are looking into my problem

This is the gdscipt code of the units:

extends KinematicBody

var path = []
var path_rot = []
var path_ind = 0
var ROTATE_SPEED = 10
const move_speed = 12
onready var nav = get_parent()

func move_to(target_pos):
	path = nav.get_simple_path(global_transform.origin, target_pos)
	path_ind = 0
	

func _physics_process(delta):
	if path_ind < path.size():
		var move_vec = (path[path_ind] - global_transform.origin)
		if move_vec.length() < 0.1:
			path_ind += 1
		else:
			move_and_slide(move_vec.normalized() * move_speed, Vector3(0, 1 ,0))
			#look_at(transform.origin - move_vec.normalized(), Vector3.UP)
			var t = global_transform
			var lp = t.origin + move_vec.normalized()
			var lookatpos = lp
			var l = t.looking_at(lookatpos, Vector3(0,1,0))
			var a = Quat(t.basis)
			var b = Quat(l.basis)
			var c = a.slerp(b, ROTATE_SPEED * delta)
			global_transform.basis = Basis(c)

Maybe check if your global transform is at the level of the ground. Otherwise print out the simple path and see if the y part of the vector is above ground level, and check if the two points that are used for get simple path are above ground level.

Thank you

I checked and the global transform was at 1, after moving the Ground node (above my nav node) to 0 and instead placing the navmesh node at -1 it almost looked right. I then reduced the cell height in the navmesh to 0.1 and disabled the rotation of my unit now it looks right. :)

But am I supposed to set the cell height lower than the default to touch the ground ? :/ Seems like setting the height lower than default creates another problem, because as soon as I add the rotation back in again (via look_at or the code seen at the end of my code sample) the unit just falls to the ground and is stuck. Even the default 0.2 gives me this strange behaviour.

https://imgur.com/LtnC3tH

That doesn't sound right. What about the model? Does it have it's own scene. Is it set up so it's at 0,0,0? Also, when they did the baking of the navmesh in the tutorial, make sure you follow it closely for the settings, etc. The odd thing is it moves up when it gets a path from navmesh. I think you are just compensating for it, but that doesn't really solve the problem. Watch the tutorial again carefully and see if there might be a setting in the editor you missed during the navmesh setup. Are the models yours or did you download them with the tutorial?

Thanks again for your time

Yes they are my models, but I also tried creating basic shapes with godot and they also are floting if I set thier lowest point to be at Y=0. The offset seems to be 0.4 (with default cell heigth) Seems like the distance from ground is always 2x the cell height, as it changes from 0.4 to 0.2 if I change the cell height to 0.1, but the path doesn

BUT I have found out why the units were stuck. It happens when I assign the collision mask of the ground object. I watched the tutorial again, as suggested and noticed that the units are on a different collision layer and are also not using the collision mask of the ground.

Now they move as intended, but im still not sure if I should lower the navmesh cell height to fix my floating objects issue or if there is some other solution.

EDIT: I found a work around, if I set

var move_vec = (path[path_ind] - global_transform.origin)

to


var move_vec = (path[path_ind] - global_transform.origin - Vector3(0, 0.4, 0))

then I can just remove the double of the cell size of 0.2 form my movement vector.

Glad you got it working. I think that work around would be all right.

a year later