Character 3D going up and down unintentionally when over my voxels

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

I’m programming a first person character for a 3D game. I followed the tutorial from KidsCanCode. Here is my code:

extends KinematicBody

onready var camera : Camera = $HeadPivot/Camera

var gravity :float = -30.0
var max_speed : float = 8.0
var jump_speed :float = 12.0
var jump : bool = false
var mouse_sensitivity : float = 0.002 # radians/pixel

var mouse_captured : bool = false

var velocity : Vector3 = Vector3()

func _init():
	# capture mouse
	mouse_captured = true

func get_input():
	# we want to move in the direction that the camera is facing
	# we maybe already moving
	var input_direction : Vector3 = Vector3()
	jump = false
	if Input.is_action_just_pressed("jump"):
		jump = true
	if Input.is_action_pressed("move_forward"):
		input_direction -= camera.global_transform.basis.z
	if Input.is_action_pressed("move_backwards"):
		input_direction += camera.global_transform.basis.z
	if Input.is_action_pressed("move_rightwards"):
		input_direction += camera.global_transform.basis.x
	if Input.is_action_pressed("move_leftwards"):
		input_direction -= camera.global_transform.basis.x
	input_direction = input_direction.normalized()
	return input_direction

func _unhandled_input(event):
	# rotation
	if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		# avoid turn
		$HeadPivot.rotation.x = clamp($HeadPivot.rotation.x, -1.2, 1.2)
	# mouse capture
	if event.is_action_pressed("capture_mouse") and not event.is_echo():
		mouse_captured = not mouse_captured
		if not mouse_captured: # if not capture, release it

func _physics_process(delta):
	velocity.y += gravity*delta
	if jump and is_on_floor():
		velocity.y += jump_speed
	var desired_velocity = get_input()*max_speed
	velocity.x = desired_velocity.x
	velocity.z = desired_velocity.z
	velocity = move_and_slide(velocity, Vector3.UP, true)

It works more or less. When the character is over a surface of many voxels, strange things happen, see this video. CollisionShapes are shown. This may also help: video.

The voxels CollisionShapes are created using Mesh.create_trimesh_shape(). Only visible faces are computed.

Any idea? Thanks!


Tree structure:

  • GameManager

    • Experimental
      • VoxelsContainer (here there are all voxels as children, StaticBodys)
      • Character (KinematicBody)
        All voxels are children of VoxelsContainer, even the one in the middle of the floor. It seems that the problem arise when there is a flat surface of voxels.

create a new scene. Or clean the scene. And add two Static Body. One for the floor and one for the cube (upstairs) and try again

Or set the player’s initial position on the scene relative to the floor and try again

ramazan | 2022-01-13 10:48

See the edit. That is not the tree structure: all voxels are children of an Spatial and they are StaticBodys. The character is a sibling of that Spatial and it is a KinematicBody. I tried to put the character inside the voxels container, but the problem persists.

abelgutierrez99 | 2022-01-13 20:33