Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | amaou310 |
I am trying to implement knockback to my 2d platformer game project. I would like to disable player input once player enter the enemy hitbox area2d. I control player input in func get_input_direction(). I followed state machine by Nathan Lovato btw.
Player Script:
class_name Player
extends KinematicBody2D
var speed = 200
var acceleration = 30
var friction = 15
var gravity = 1500
var air_jump = true
var jump_strength = 570
var air_jump_strength := 550
var air_friction = 1
var direction := 1
var knockback := -150
var hitstate = false
var can_move = true
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
direction == -1
else:
position2D.scale.x=-1
direction == 1
func walk_flip():
if get_global_mouse_position().x > $Position2D/PlayerSkinIK.global_position.x:
position2D.scale.x=1
else:
position2D.scale.x=-1
if is_on_floor() and position2D.scale.x==1 and Input.is_action_pressed("move_right"):
_animation_player.play("Player-Run IK")
elif is_on_floor() and position2D.scale.x==-1 and Input.is_action_pressed("move_right"):
_animation_player.play("Player-Run IK Backward")
elif is_on_floor() and position2D.scale.x==-1 and Input.is_action_pressed("move_left"):
_animation_player.play("Player-Run IK")
elif is_on_floor() and position2D.scale.x==1 and Input.is_action_pressed("move_left"):
_animation_player.play("Player-Run IK Backward")
func pass_through():
set_collision_mask_bit(4, false)
print("mask_off")
yield(get_tree().create_timer(0.1), "timeout")
set_collision_mask_bit(4, true)
print("mask_on")
func _on_HurtboxPlayer_area_entered(area: Area2D):
if area.name == "HitboxMushroom01":
direction == -1
print("ouch right")
_velocity.y += -120
_velocity.x = direction * 280
if area.name == "HitboxMushroom02":
direction == 1
print("ouch left")
_velocity.y += -120
_velocity.x = direction * -280
pass
Move State:
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 = lerp(player._velocity.x, player.get_input_direction() * player.speed, player.acceleration * delta)
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")