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:
https://youtu.be/Eups1JSqn7k
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 maxspd = 10000
onready var player = getparent().get_node("Player")
func _process(delta):
$AnimatedSprite.play("outline")
func physicsprocess(delta):
var direction = (player.globalposition - globalposition).normalized()
direction = moveandslide(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():
screensize = getviewportrect().size
func process(delta):
var p
while count == 0:
p = pistolPath.instance()
getparent().addchild(p)
p.globalposition = 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 physicsprocess(delta):
var movedirectionH = int(Input.isactionpressed("RightAlt")) - int(Input.isactionpressed("LeftAlt"))
velocity.x = spd * movedirectionH * 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 = globalposition
getparent().add_child(bullet)