#My character keeps getting dragged away with the enemy's knockback when attacking from above.

9 messages · Page 1 of 1 (latest)

primal walrus
#

No idea how this happened, but i spent a few DAYS figuring out the knockback on my enemy (who is a default box rn dw about it) but for some reason, when I test it, from above it will like grab the player and drag them along . It only seems to happen when the player is extremely close to the enemy..

something tells me it has something to do with the collision shapes, not the code, but i have played with them and nothing changes.

Ill put my code below, but tbh people who have seen it have been baffled as to how the code could be involved. I'm stumped. ;w;

#
# movement variables
const max_speed = 200
const accel = 900
const friction = 750

enum {idle,walk,attack}
var state = idle
var input_vector = Vector2.ZERO
var blend_position : Vector2 = Vector2.ZERO
var blend_pos_paths = [
    "parameters/idle/idle_bs2d/blend_position",
    "parameters/walk/walk_bs2d/blend_position",
    "parameters/attack/BlendSpace2D/blend_position"
]
var animTree_state_keys = [
    "idle",
    "walk",
    "attack"
]

#anim variables
@onready var animationTree = $AnimationTree
@onready var state_machine = animationTree["parameters/playback"]
@export var bat_get = true

#attack_var
var knockback_dir = Vector2()
var knockback_wait = 30
var knockback_str = 2
var damage = 0
var element1 = "none"
var element2 = "none"
signal knockback
#movement code

func _physics_process(delta):
    move(delta)
    animate()

func move(delta):
    input_vector = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    if input_vector == Vector2.ZERO:
        state = idle
        apply_friction(friction * delta)
    else:
        state = walk
        apply_movement(input_vector * accel * delta)
        blend_position = input_vector
    move_and_slide()
    if bat_get == true:
        if Input.is_action_just_pressed("ui_accept"):
            state = attack
            $Area2D/Hamelin_attack_area.set_deferred("disabled",false)
            
        else:
            $Area2D/Hamelin_attack_area.set_deferred("disabled",true)
            
    else:
        pass
    for body in $Area2D.get_overlapping_bodies():
        if knockback_wait <= 0 and body.get("NAME") == "enemy":
            emit_signal("knockback")
            knockback_wait = 30
    knockback_wait -= 1
func apply_movement(amount) -> void:
    velocity += amount
    velocity = velocity.limit_length(max_speed)

func apply_friction(amount) -> void:
    if velocity.length() > amount:
        velocity -= velocity.normalized() * amount
    else:
        velocity = Vector2.ZERO



func animate() -> void:
    state_machine.travel(animTree_state_keys[state])
    animationTree.set(blend_pos_paths[state], blend_position)
#

const NAME = "enemy"

var knockback = false
var knockback_power = 40
var friction = 750
var motion = Vector2()

func _ready():
    pass

func _physics_process(_delta):
    pass

func _on_hamelin_knockback():
    knockback = true


func _on_area_2d_area_entered(area):
    if knockback == true:
        var knockback_dir = $"../Hamelin".global_position.direction_to(global_position) * knockback_power
        var target_postition = global_position + knockback_dir
        var getback = create_tween() 
        getback.tween_property(self,"global_position",target_postition,0.2)
        knockback = false
#

player is first, the enemy is second.

cedar garnet
#

The default setting Grounded is meant for platformers and has floor detection etc, which will trigger some built-in behavior that'll lead to weird behaviors

#

One of those little hidden quirks in godot 🙁

primal walrus
#

OHHHH

#

thank you ill see if that's what is going on and i will share this with people who were stumped if thats what this is!