+1 vote

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 moveandcollide()

in Engine by (49 points)
edited by

1 Answer

+2 votes

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.

by (20,917 points)

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

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.