how to remove the, gravity of a kinematic body

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

i have kinematic body i want i want to move it but if i use move_and_slide or move_and_collide there is a gravity how how can i remove the gravity

usually you define the vector for move_and_foobar yourself. Can you show us your code? (Whole function/method)

whiteshampoo | 2020-05-28 06:41

:bust_in_silhouette: Reply From: njamster

“Kinematic bodies are special types of bodies that are meant to be user-controlled. They are not affected by physics at all” (Source) If there is gravity, it is there because you added it! As you haven’t provided any code, this will be the answer for now.

here is my code


extends KinematicBody2D
var velocity = Vector2(0, 0 )
var speed = 750
signal shoot
signal  shoot_realsed
func _physics_process(delta):
	if Input.is_action_pressed("droite"):
		velocity.x = 5 * -speed 
	elif Input.is_action_pressed("gauche"):
		velocity.x = 5 * speed 
	elif Input.is_action_pressed("ui_up"):
		velocity.y = 5 * speed
	elif Input.is_action_pressed("shoot"):
		emit_signal("shoot")
	elif Input.is_action_just_released("shoot"):
		emit_signal("shoot_realsed")
	move_and_collide(velocity)

sphixy011 | 2020-05-28 12:33

And what exactly do you perceive as gravity here?

  • If you press any key associated with the InputAction called “droite”, the player’s velocity in x-direction is set to 5 * -750 = -3750, so the player will move to the left (which feels a bit weird, given that “droite” means “right” in french).
  • If you don’t press any key associated with “droite”, the script will go on and check if you press any key associated with “gauche” and if that’s the case, the player will move to the right with a velocity of 3750 in x-direction.
  • If you don’t press any key associated with neither “droite” nor “gauche”, but a key associated with “ui_up” the player’s velocity is set to 3750 in y-direction. The positive y-direction is pointing downwards, so the player will “fall”. That’s not gravity though, but a misunderstanding on your side at most.

You don’t reset the velocity back to zero when none of these keys are pressed, which may or may not be intended. Also you’re not multiplying the velocity with delta, so your character will move extremely fast on high FPS and slower on lower FPS.

njamster | 2020-05-28 14:06