It is me again with another collision question. ( I know, shocking ) Anyway, while I was working on my game I encountered a problem with the enemy collision. On my original test scene the enemy collision works just fine. The player will get hurt by the enemy and will die in three hits. However, when I moved over the player and the enemy over to another scene ( any old or new scene ) the enemy won't hurt the player. I originally thought it was a problem with the tilemaps ( for I made a new one for the new scenes ), but I figured out after some testing that it won't work at all. I did start to use collision layers to help control collision, but the hurt still works fine on the original scene. The script for the player can be seen below.
extends KinematicBody2D
var movement = Vector2()
var up = Vector2(0,-1)
var isAttacking = false
var facing_right = true
var dead = false
var hurt = false
var knockback = 300
const GRAVITY = 20
const ACCEL = 11
const MAXFALLSPEED = 250
const MAXSPEED = 130
const JUMPFORCE = 405
const NEWGRAVITY = 10
const MAXGLIDER = 150
func _ready():
var tilemap_rect = get_parent().get_node("TileMap").get_used_rect()
var tilemap_cell_size = get_parent().get_node("TileMap").cell_size
$Camera2D.limit_left = tilemap_rect.position.x * tilemap_cell_size.x
$Camera2D.limit_right = tilemap_rect.end.x * tilemap_cell_size.x
$Camera2D.limit_top = tilemap_rect.position.y * tilemap_cell_size.y
$Camera2D.limit_bottom = tilemap_rect.end.y * tilemap_cell_size.y
pass
func _process(delta):
if dead == false && hurt == false:
movement.y += GRAVITY
if movement.y > MAXFALLSPEED:
movement.y = MAXFALLSPEED
if facing_right == true:
$AnimatedSprite.scale.x = 1
else:
$AnimatedSprite.scale.x = -1
movement.x = clamp (movement.x, -MAXSPEED, MAXSPEED)
if Input.is_action_just_pressed("ui_right"):
get_node("HitBox").set_scale(Vector2(1,1))
elif Input.is_action_just_pressed("ui_left"):
get_node("HitBox").set_scale(Vector2(-1,1))
if Input.is_action_pressed("ui_right") &&isAttacking == false:
movement.x += ACCEL
facing_right = true
if is_on_floor() == true:
$AnimatedSprite.play("Move")
elif Input.is_action_pressed("ui_left") && isAttacking == false:
facing_right = false
movement.x -= ACCEL
if is_on_floor() == true:
$AnimatedSprite.play("Move")
else:
movement.x = lerp (movement.x, 0, 0.2)
if isAttacking ==false && is_on_floor() == true:
$AnimatedSprite.play("Idle")
if is_on_floor() && isAttacking == false:
if Input.is_action_just_pressed("ui_up"):
movement.y = -JUMPFORCE
if !is_on_floor():
if movement.y < 0 :
$AnimatedSprite.play("Jump")
elif movement.y > 0:
$AnimatedSprite.play("Fall")
if Input.is_action_pressed("Attack") && is_on_floor() && hurt == false:
$AnimatedSprite.play("Attack")
isAttacking = true
$HitBox/CollisionShape2D.disabled = false
if Input.is_action_pressed("Glide"):
if is_on_floor() == false:
$AnimatedSprite.play("Glide")
movement.y += NEWGRAVITY
if movement.y >= MAXGLIDER:
movement.y = MAXGLIDER
if Input.is_action_pressed("Restart"):
get_tree().reload_current_scene()
movement = move_and_slide(movement, up * delta )
func _on_AnimatedSprite_animation_finished():
if $AnimatedSprite.animation == "Attack":
$HitBox/CollisionShape2D.disabled = true
isAttacking = false
if $AnimatedSprite.animation == "Dead":
Globals.health += Globals.MaxHealth
get_tree().reload_current_scene()
func _on_DeathBox_body_entered(body):
if body == self and hurt == false and isAttacking == false:
modulate.a = 0.5
$AnimatedSprite.play("Hurt")
$Slime_Timer.start()
hurt = true
Globals.health -=1
if Globals.health <=0:
$AnimatedSprite.play("Dead")
func _on_Slime_Timer_timeout():
hurt = false
$Slime_Timer2.start()
func _on_Slime_Timer2_timeout():
modulate.a = 1