0 votes

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 = get
parent().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():
screen
size = getviewportrect().size

func process(delta):
var p
while count == 0:
p = pistolPath.instance()
get
parent().addchild(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 physicsprocess(delta):
var movedirectionH = int(Input.isactionpressed("RightAlt")) - int(Input.isactionpressed("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 = globalposition
get
parent().add_child(bullet)

Godot version v.3.3.2
in Engine by (46 points)

1 Answer

0 votes

The position of the player seems to get messed up by the enemy ai. Does it work to change to "player.position" instead of the global one?

by (179 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.