![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | Matt101 |
Hi everyone, i have a problem with my top down shooter prototype, and i can’t understand what happened. I want to make the Enemy chase the Player, so even after followed a lot of tutorials and read a lot of discussions in reddit i coudn’t found out what happened wrong.
It’s kinda hard to explain what happened, so i uploaded a video on youtube with the
game running, check out:
I just wanna know how i can fix that. Thanks in advance.
Slime (Enemy)
-------> AnimatedSprite
-------> CollisionShape2D
Player
-------> AnimatedSprite
-------> CollisionShape2D
-------> Position2D
Enemy Code
extends KinematicBody2D
var max_spd = 10000
onready var player = get_parent().get_node(“Player”)
func _process(delta):
$AnimatedSprite.play(“outline”)
func _physics_process(delta):
var direction = (player.global_position - global_position).normalized()
direction = move_and_slide(direction * max_spd * delta)
Player Code
extends KinematicBody2D
var velocity = Vector2()
var spd = 15000
var detectDirection = 0
var canFire = true
var count = 0
var screen_size
var bulletPath = preload(“res://Bullet.tscn”)
var pistolPath = preload(“res://Pistol.tscn”)
func _ready():
screen_size = get_viewport_rect().size
func _process(delta):
var p
while count == 0:
p = pistolPath.instance()
get_parent().add_child(p)
p.global_position = position
count += 1
if Input.is_action_just_pressed("Fire") and canFire == true:
fire()
if velocity.x < 0:
$AnimatedSprite.animation = "Walk"
$AnimatedSprite.play("Walk")
$AnimatedSprite.flip_h = true
if velocity.x > 0:
$AnimatedSprite.animation = "Walk"
$AnimatedSprite.play("Walk")
$AnimatedSprite.flip_h = false
func _physics_process(delta):
var move_directionH = int(Input.is_action_pressed(“RightAlt”)) - int(Input.is_action_pressed(“LeftAlt”))
velocity.x = spd * move_directionH * delta
var move_directionV = int(Input.is_action_pressed("DownAlt")) - int(Input.is_action_pressed("UpAlt"))
velocity.y = spd * move_directionV * delta
if velocity.x == 0 and velocity.y == 0:
$AnimatedSprite.play("Idle")
else:
$AnimatedSprite.stop()
move_and_slide(velocity)
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
func fire():
var bullet = bulletPath.instance()
bullet.position = global_position
get_parent().add_child(bullet)