Hii there,
I'm trying to make a little game (duh) and now I came to the part where I make some punches.
Right now, the punch keeps going while it is pressed, but I want it to have a single punch for 1second (and make it so that the player can't input any other actions while punching) and then reset to the initial state (so just standing there)
This is the code I have at the moment:
extends KinematicBody2D
const GAME_DIRECTION_UP = Vector2(0,-1)
export var movement_speed = 512
export var gravity_force = 64
export var jump_force = 1024
var is_Crouching = false
var facing_Direction = "Left"
var motion = Vector2()
func _ready():
print("Game ready")
pass
func _physics_process(delta):
###
# Gravity
###
motion.y += gravity_force
$Sprite.set_position(Vector2(0,0)) # Reset the sprite position
###
# Movement
###
# Left-Right movement
if(Input.is_action_pressed("ui_left")):
if(!is_Crouching):
# Left button is pressed
motion.x = -movement_speed
ChangeDirection("Left")
elif(Input.is_action_pressed("ui_right")):
if(!is_Crouching):
# Right button is pressed
motion.x = movement_speed
ChangeDirection("Right")
else:
motion.x = 0
# Jumping
if is_on_floor():
if(Input.is_action_just_pressed("ui_up") && !is_Crouching):
motion.y = -jump_force
# Crouching
if Input.is_action_pressed("ui_down"):
is_Crouching = true
$Sprite.set_texture(load("res://Sprites/player_crouching.png")) # Change the sprite
$Collision.set_scale(Vector2(4.25,5.4328485)) # Change the Collisionbox size
$Collision.set_position(Vector2(0,57)) # Change Collisionbox position
else:
is_Crouching = false
$Sprite.set_texture(load("res://Sprites/player_standing.png")) # Change the sprite
$Collision.set_scale(Vector2(4.25,10.865697)) # Change the Collisionbox size
$Collision.set_position(Vector2(0,0)) # Change Collisionbox position
###
# Attacks
###
if(Input.is_action_pressed("p1_sp")):
if(is_Crouching):
$Sprite.set_texture(load("res://Sprites/player_crouching_punch.png")) # Change the sprite
$Sprite.set_position(Vector2(18,0)) # Re-center the sprite in the collisionbox
else:
$Sprite.set_texture(load("res://Sprites/player_standing_punch.png")) # Change the sprite
$Sprite.set_position(Vector2(18,0)) # Re-center the sprite in the collisionbox
motion = move_and_slide(motion,GAME_DIRECTION_UP)
func ChangeDirection(direction):
facing_Direction = direction
if facing_Direction == "Left":
$Sprite.set_flip_h(true)
elif facing_Direction == "Right":
$Sprite.set_flip_h(false)