+1 vote

enter image description here

As you can see from the gif, player hurtbox collision often does not work as intended and I have no idea why. I would appreciate if you could help me to solve this problem. I am guessing this problem is due to the object moving too fast.

Both hitbox and hurtbox are the area2d and collisionshape2d.
enter image description here

Player script:

class_name Player

extends KinematicBody2D

var speed = 200
var jump_strength = 570
var air_jump_strength := 550
var air_jump = true
var _jumps_made := 0
var gravity = 1500
var acceleration = 60
var friction = 20
var air_friction = 10
var dir := 1
var knockbackdir := 1
var knockback := -150

var _velocity := Vector2.ZERO

onready var position2D = $Position2D
onready var _animation_player: AnimationPlayer = $Position2D/PlayerSkinIK/AnimationPlayer


func get_input_direction() -> float:
    var _horizontal_direction = (
        Input.get_action_strength("move_right")
        - Input.get_action_strength("move_left")
    )
    return _horizontal_direction

func _process(delta: float) -> void:
    if get_global_mouse_position().x > $Position2D/PlayerSkinIK.global_position.x:
        position2D.scale.x=1
        dir == -1
        knockbackdir == -1
        print("right")
    else:
        position2D.scale.x=-1
        dir == 1
        knockbackdir == 1
        print("left")


func _on_HurtboxPlayer_area_entered(area: Area2D):
    if area.name == "HitboxMushroom01":
        knockbackdir == -1
        print("ouch")
        _velocity.y += -600
        _velocity.x = knockbackdir * knockback
        pass

Move State Script:

extends PlayerState

export (NodePath) var _animation_player
onready var animation_player: AnimationPlayer = get_node(_animation_player)


func enter(_msg := {}) -> void:
    pass

func physics_update(delta: float) -> void:
    player.walk_flip()

    if not player.is_on_floor(): #If player is not on floor, FALL STATE
        state_machine.transition_to("Fall")
        return

    if not is_zero_approx(player.get_input_direction()): #If Player is moving, calculate Player's velocity.x 
        player._velocity.x = player.get_input_direction() * player.speed

    player._velocity.y += player.gravity * delta #Gravity Calculation
    player._velocity = player.move_and_slide(player._velocity, Vector2.UP)


    if Input.is_action_just_pressed("jump"): #If input jump pressed, JUMP STATE
        if Input.is_action_pressed("ui_down"):
            state_machine.transition_to("JumpDown")
        else:
                state_machine.transition_to("Jump")
    elif is_zero_approx(player.get_input_direction()): #If Player is Not Moving, IDLE STATE
        state_machine.transition_to("Idle")
Godot version ver3.3.3
in Engine by (82 points)
edited by

I have trouble seeing what is going wrong, since you haven't explained what you are trying to accomplish. It will be hard to help you with your problem if we don't know what you intend to do and what is going wrong

My apologies for not being clear. I want my player to be bounced off from an enemy every time when hurtbox entered hitbox. However, player's hurtbox often ignores collision or bounce upward instead. Do you have any idea why this problem cause.

1 Answer

0 votes

I bet your knockback code collides with your movement code. Show the movement code.

by (8,101 points)

Thank you Inces. Please do check added movement script.

Yes, this is what I suspected.

When player is moving, You arbitrary set its velocity.x to the value calculated from input only.
But when collision occurs, You arbitrary set velocity.x to another value, calculated by knockback only.
These calculations are independent, so horizontal velocity will behave like on/off. Either player is moving, either player is colliding.

Look how You did it with velocity.y. Nothing is arbitrary set, instead You add gravity force value to knockback value, and it works smoothly.
Do the same thing with velocity.x. Design it so the velocity.x is calculated in one line of code using values from two sources.

I would appreciate if you could kindly teach me how to implement that. It is beyond my understanding since I am very new to godot.

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 Frequently asked questions and 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 [email protected] with your username.