Problem with KinematicBody2D

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

I made a moving platform which is kinematicbody2d and when a player (which is a kinematicbody2d too) stands on it moving platform is going down. I don’t know why.

Moving platform script:

   extends KinematicBody2D

var velocity = Vector2.ZERO
var x_direction = 1
var x_speed = 30
var y_speed = 1
var y_direction = 1

func _physics_process(delta):
	velocity.x = x_speed * x_direction
	var collision = move_and_collide(velocity * delta)
	if collision != null and collision.collider.is_in_group("Ground"):
		if x_direction == 1:
			x_direction = -1
		elif x_direction == -1:
			x_direction = 1
	velocity.y = y_speed * y_direction
func _on_Timer_timeout():
	y_direction = -1

func _on_Timer2_timeout():
	y_direction = 1

did you make collision shape for the platform?

hero2002 | 2020-06-15 21:30

Yes i did make a collision shape.

smetek | 2020-06-15 21:41

i think you shouldnt make the platform with kinematicbody2d use instead staticbody2d or if you have tileset make tilemap

hero2002 | 2020-06-15 21:46

:bust_in_silhouette: Reply From: Scavex

What is it that you want to do with this platform though ? If you just want it to move around x-axis then simply do this change

var y_speed = 0

the platform will not go down if the player is standing on top of it now.