Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | ThreeSpark |
I am having trouble detecting collision with a Kinematic Body2D and an Area2D. Here is the code for collisions:
for idx in range(get_slide_count()):
var collision = get_slide_collision(idx)
if collision.collider.is_in_group("meteor"):
var playspace = playsp.instance()
playspace.playing = false
any suggestions? here is the full code if you need it:
extends KinematicBody2D
var velocity = Vector2()
var speed = 10
var immune = false
const playsp = preload("res://playspace.tscn")
func _ready():
$AnimationPlayer.play("start_animation")
$Particles2D2.emitting = false
$LaserReadyTimer.start()
func get_input():
var up = Input.is_action_pressed("up")
var down = Input.is_action_pressed("down")
var laser = Input.is_action_just_pressed("laser")
if laser:
laser()
if up:
velocity.y -= speed
if down:
velocity.y += speed
func laser():
if $Particles2D3.lifetime <= 0.99:
return
$Particles2D2.emitting = true
$LaserTimer.start()
$Particles2D3.lifetime = 0.1
immune = true
func progress_bar():
if $Particles2D3.lifetime >= 1:
return
$Particles2D3.lifetime += 0.001
func _process(delta):
get_input()
if velocity.y < 0:
velocity.y += 1
if velocity.y > 0:
velocity.y -= 1
$Particles2D.lifetime += 0.0002
progress_bar()
if position.y <= 0:
position.y = 575
if position.y >= 576:
position.y = 0
func _physics_process(delta):
move_and_slide(velocity)
if velocity.y >= 10000:
velocity.y = 10000
for idx in range(get_slide_count()):
var collision = get_slide_collision(idx)
if collision.collider.is_in_group("meteor"):
var playspace = playsp.instance()
playspace.playing = false
func _on_LaserTimer_timeout():
$Particles2D2.emitting = false
immune = false