Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | VietAnhT |
I’m making battle city like game without grid movement and having trouble with bullet collision.
1: Outer angle tile collision, bullet cant detect collided tile the bullet disappear and tile not remove.
2: Remove both bullet when bullet collision, some time both removed and some time only one removed
Here is my bullet code:
extends KinematicBody2D
var speed = 500
var velocity = Vector2()
func start(pos, dir):
rotation = dir
position = pos
velocity = Vector2(speed, 0).rotated(dir-PI/2)
func _physics_process(delta):
var collision = move_and_collide((velocity * delta).round())
if collision:
print(collision.collider.name)
velocity = velocity.bounce(collision.normal)
if collision.collider is TileMap:
var tile = collision.collider.world_to_map(collision.position - velocity.normalized())
if collision.collider.get_cellv(tile) == 0:
collision.collider.set_cell(tile.x, tile.y, -1)
elif collision.collider.name == "Enemy" or collision.collider.name == "Player":
collision.collider.queue_free()
queue_free()
func _on_VisibilityNotifier2D_screen_exited():
queue_free()
Edit: Look like i use move_and_collide() wrong??
Edit 2: 2nd problem solved by add hit() method to all object