kidscancode/godot_tutorials

is_colliding does not works

m0agh opened this issue · 3 comments

m0agh commented

I'm following your awesome tutorials about up and running godot engine. in part 10, you wrote a code to avoid character jumping to sky with holding ui_up key with adding RayCast2D. but for me. if i hold arrow up key first time it well be fine but after landing on the ground and jumping again(i'm still holding up key) the character goes to sky! even if write set_process_input(true)!

the code:

      extends KinematicBody2D  
      onready var ground_ray = get_node("gr")

const ACCEL = 1500
const MAX_SPEED = 400
const FRICTION = -400
const GRAVITY = 2000
const JUMP_SPEED = -300

var acc = Vector2()
var vel = Vector2() 

func _ready():
	set_fixed_process(true)
	set_process_input(true)

func _input(event):
	if Input.is_action_pressed("ui_up") and ground_ray.is_colliding():
		vel.y = JUMP_SPEED 

func _fixed_process(delta):
	acc.y = GRAVITY
	acc.x = Input.is_action_pressed("ui_right") - Input.is_action_pressed("ui_left")
	acc.x *= ACCEL
	if acc.x == 0:
		acc.x = vel.x * FRICTION * delta

	vel += acc * delta
	vel.x = clamp(vel.x, -MAX_SPEED,MAX_SPEED)
	
	var motion = move(vel * delta)
	if is_colliding():
		var n = get_collision_normal()
		motion = n.slide(motion)
		vel = n.slide(vel)
		move(motion) `

ezgif-4-250626b8a4

m0agh commented

oops, i forget to change Input to event in new added function, the jumping issue with holding up key are solved. but when the jump starts, if i press up key in repeating sequence, the character goes up and up again.

You need to check your ground ray. That's the thing that's checking if a jump should be allowed. If it's sticking out too far (cast_to property) then it will still be hitting the ground even when you're up in the air. Also, if it's touching the player's CollisionShape, then it counts as colliding all the time. You can either adjust its position, or add the player as an exception by adding this to _ready():

ground_ray.add_exception(self)
m0agh commented

There was a connection between the collisionShape and the rayCast.
Thanks