How do I use move_and_slide to check for collision with enemies?

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

Entities code:

extends KinematicBody2D

export var toggle_gravity_once = false
var just_toggle_gravity_once = false


export var toggle_move_once = false
var just_toggle_move_once = false

export var toggle_collision_once = false
var just_toggle_collision_once = false

export var toggle_running_once = false
var just_toggle_running_once = false

var debug = false



export var toggles_left = 3
export var self_toggles_left = 0
export var other_toggles_left = 0






var gravity_just_off = false

export var speed = 250


export var gravity_scale = 1
export var gravity = 500
var gravity_on = true
var gravity_toggle = false
var running_on = true
var move_on = true
var collision_on = true

var feet_touch = false
var feet_exit_touch = false
var bodies_feet_touch = []
var bodies_feet_exit = []
var up_touched = []
var up_exited = []
var left_touched = []
var left_exited = []
var right_touched = []
var right_exited = []

var init_pos 



var right_vector = Vector2(1,0)

var velocity = Vector2()


class Entity_Input:
	var move_h = 0
	var move_v = 0
	var toggle_gravity = false
	var toggle_running = false
	
	var toggle_health = false
	var toggle_collision = false
	var toggle_global = false
	var toggle_other = false
	var toggle_self = false
	var toggle_move = false
	
var entity_input = Entity_Input.new()

class Health:
	var current = 3
	
	func _init(c = current):
		current = c
		
	func deplete_health(amount):
		current -= amount
		
	func any_left():
		if(current > 0):
			return true
		else:
			return false
		return false


var health = Health.new()

func _ready():
	init_pos = position
	set_collision_mask_bit(0, true)
	add_to_group("Entity")
	pass

func _process(delta):
	pass

func _physics_process(delta):
	get_input()
	gravity_just_off = false
	toggle_mechanics()
	var cols = []
	if(move_on):
		move(delta)
	
	if(is_in_group("Enemy")):
		for i in range (get_slide_count()):
			if(get_slide_collision(i).collider.is_in_group("Player")):
					pass
					get_slide_collision(i).collider.queue_free()
	if(is_in_group("Player")):
		for i in range (get_slide_count()):
			if(get_slide_collision(i).collider.is_in_group("Enemy")):
					pass
					queue_free()
	
		

	if debug:
		$Label.text = "Velocity: " +  str(velocity.length()) + "Health: " + str(health.current) + "Toggles left: " + str(toggles_left) + str(cols)
	
	pass
func move(delta):
	
	velocity = Vector2()
	if(gravity_on):
		 entity_input.move_v = 0
	var run_vec = Vector2()
	if(running_on):
		run_vec = running(delta, entity_input.move_h, entity_input.move_v)
	
	velocity += run_vec * speed
	if(gravity_on):
		velocity += gravity()
	
	
	move_and_slide(velocity)
	
	

#
func running(delta, move_h, move_v):
	if(abs(move_h) == 1 and abs(move_v) == 1):
		return Vector2(move_h, move_v)/sqrt(2)
	else:
		return Vector2(move_h, move_v)
	pass

func attack(delta):
	pass
	
func gravity():
	for i in bodies_feet_touch.size():
		if(bodies_feet_touch[i] != null):
			if(bodies_feet_touch[i].is_class("StaticBody2D")):
				entity_input.move_v = 0
				velocity = Vector2(velocity.x, 0)
				return Vector2()
	return Vector2(0, 1) * gravity * gravity_scale
	pass




func _on_CheckFeet_body_entered(body):
	
	bodies_feet_touch.append(body)


func _on_CheckFeet_body_exited(body):
	
	
	bodies_feet_touch.erase(body)
	
	pass # replace with function body


func _on_Up_body_entered(body):
	up_touched.append(body)
	
	pass # replace with function body


func _on_Up_body_exited(body):
	up_touched.erase(body)
	pass # replace with function body


func _on_Right_body_entered(body):
	right_touched.append(body)
	pass # replace with function body


func _on_Right_body_exited(body):
	right_touched.erase(body)
	pass # replace with function body


func _on_Left_body_entered(body):
	left_touched.append(body)
	pass # replace with function body


func _on_Left_body_exited(body):
	left_touched.erase(body)
	pass # replace with function body

And here is the enemies code.

extends "res://scripts/Entity.gd"

func _ready():
	._ready()
	add_to_group("Enemy")
	entity_input.move_h = 1
	
	pass

func get_input():
	for i in right_touched.size():
		if(right_touched[i].is_class("StaticBody2D") or (right_touched[i].is_class("KinematicBody2D") and right_touched[i] != self )):
			entity_input.move_h = -1
	for i in left_touched.size():
		if(left_touched[i].is_class("StaticBody2D") or (left_touched[i].is_class("KinematicBody2D") and left_touched[i] != self)):
			entity_input.move_h = 1
		pass
	
	.get_input()
	pass

Here is the enemy:
enter image description here

And here is player code:

extends "res://scripts/Entity.gd"



func _ready():
	add_to_group("Player")
	._ready()
	pass

func get_input():
	entity_input.move_h = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
	entity_input.move_v = int(Input.is_action_pressed("down")) - int(Input.is_action_pressed("up"))
	entity_input.toggle_gravity = Input.is_action_just_pressed("toggle_gravity")
	entity_input.toggle_running = Input.is_action_just_pressed("toggle_running")
	entity_input.toggle_move = Input.is_action_just_pressed("toggle_move")
	
	
	entity_input.toggle_global = true
	entity_input.toggle_other = Input.is_action_pressed("toggle_other")
	entity_input.toggle_self = Input.is_action_pressed("toggle_self")
	entity_input.toggle_collision = Input.is_action_just_pressed("toggle_collision")


	.get_input()

Edit:
I removed code I didn’t think was important. Hopefully that is the case.
Apparently it also doesn’t work with move_and_collide()

:bust_in_silhouette: Reply From: kidscancode

First of all, everything is moving slow because you shouldn’t multiply your velocity by delta when using move_and_slide() as it does so internally. See class docs for details.

I didn’t multiply it by delta. Also is there a way to get it to notice collision with enemies consistently.

Godotuser5675 | 2018-09-04 00:27

Well, you didn’t include any information about your speed, so I kind of have to guess what’s going on. You have a running function that you pass delta to, and there’s no way to tell what that’s doing.

Kinematic collisions are not typically tricky. You move the body and if it collides, it tells you. The thing to remember is that they only detect collisions when moved. If the player is still and the enemy moves into it, the player won’t detect a collision. So you have to check on both bodies (whichever is moving).

Aside from that, you’ll have to include more information about your setup to be able to predict what might be wrong.

kidscancode | 2018-09-04 00:34

I included some more information in the question. I removed some of the code I thought was unessential to understanding the problem. I appreciate your help.

Godotuser5675 | 2018-09-05 03:37