Godot 4.3 stable
I’m trying to make a simple fighting game prototype. When applying the same force at the same time to both of the characters one of them is delayed.
What could cause this?
Here is the script.
extends Node2D
var FLOOR_HEIGHT = 500
var velocity := Vector2.ZERO
@export var character_mirrored = 1
var character_gravity = 60
var character_move_speed = 20.0
@export var character_jump_velocity = -20
var character_stun_time = 0 #attacked by stun
var stuned = false
#const JUMP_VELOCITY = -400.0
enum State {Idle,Windup,Attack,Recovery}
var character_STATE = State.Idle
var character_hurt_vector := Vector2.ZERO
var bodies =
var b = null
var Box_Body_global=Vector4()
var Box_Body=Vector4(-50,-200,50,0)
var HBox_Body_global=Vector4()
var HBox_Body=Vector4(30,-70,120,-30)
var enemy :Node2D
var coliding_with_enemy:bool = false
var hbox_coliding:bool = false
func _ready() → void:
#scale=Vector2(character_mirrored,1)
#$Debug.scale=Vector2(character_mirrored,1)
if character_mirrored==-1:
HBox_Body.x=-HBox_Body.x
HBox_Body.z=-HBox_Body.z
HBox_Body=fixbox(HBox_Body)
func _physics_process(delta: float) → void:
coliding_with_enemy = isoverlaping(Box_Body_global,enemy.Box_Body_global)
hbox_coliding = isoverlaping(HBox_Body_global,enemy.Box_Body_global)
#print(coliding_with_enemy)
debug_update()
if Input.is_action_just_pressed("DEBUG"):
if $Debug.visible==true:
$Debug.visible = false
else: $Debug.visible=true
if global_position.y<FLOOR_HEIGHT:
velocity += Vector2(0,character_gravity) * delta
if global_position.y>=FLOOR_HEIGHT:
velocity.y=0
global_position.y=FLOOR_HEIGHT
if Input.is_action_just_pressed("ui_accept") and global_position.y>=FLOOR_HEIGHT:
velocity.y += character_jump_velocity
if Input.is_action_just_pressed("ui_up") and character_mirrored==1:
hurt(Vector2(0.2,-1),10)
velocity=Vector2.ZERO
#character_stun_time = 1
if Input.is_action_just_released("Attack1")and !stuned and hbox_coliding:
enemy.hurt(Vector2(0.5*character_mirrored,-1),30)
var direction := Input.get_axis("ui_left", "ui_right")
direction=direction*character_mirrored
if direction and !stuned:
#velocity.x = direction * SPEED
velocity.x= lerp(velocity.x,direction*character_move_speed,0.2)
else:
velocity.x= lerp(velocity.x,0.0,0.2)
#velocity.x = move_toward(velocity.x, 0, SPEED)
velocity += character_hurt_vector
#velocity.x = snapped(velocity.x,0.1)
#velocity.y = snapped(velocity.y,0.1)
move()
character_hurt_vector=Vector2.ZERO
if character_stun_time==0:
stuned = false
else:
stuned = true
character_stun_time-=1
if character_stun_time<0:
character_stun_time=0
Box_Body_global=Vector4(Box_Body.x+global_position.x,Box_Body.y+global_position.y,Box_Body.z+global_position.x,Box_Body.w+global_position.y)
HBox_Body_global=Vector4(HBox_Body.x+global_position.x,HBox_Body.y+global_position.y,HBox_Body.z+global_position.x,HBox_Body.w+global_position.y)
func hurt(vector:Vector2,a:float)->void:
character_hurt_vector+=vector*a
func debug_update() → void:
$Debug/Debug_Text.text=str(stuned)+"| Velocity: "+str(velocity)
func move() → void:
global_position+=velocity
func isoverlaping(A:Vector4,B:Vector4):
return A.x<=B.z and A.z>=B.x and A.y<=B.w and A.w>=B.y
func fixbox(a:Vector4):
var holder
if a.x>a.z:
holder = a.z
a.z=a.x
a.x=holder
if a.y>a.w:
holder = a.w
a.w=a.y
a.y=holder
return a
pass