- Edited
I've been following a really quick tutorial by a guy named MizizizizTutorials about how to do a top down game. the vid is 17 minutes long, and i've pretty much followed everything to the letter. When I just have the player in everything works fine, but its when I add my Zombie asset that the whole game/debugger crashes.
If there is anymore information you need please feel free to ask!
im not exactly sure why its doing it. This is the code I've put in, and I've copied it line for line in the tutorial:
extends CharacterBody2D
@onready var ray_cast_2d = $RayCast2D
@export var move_speed = 100
@onready var player : CharacterBody2D = get_tree().get_first_node_in_group("player")
var dead = false
func _physics_process(_delta):
if dead:
return
var dir_to_player = global_position.direction_to(player.global_position)
velocity = dir_to_player * move_speed
move_and_slide()
global_rotation = dir_to_player.angle() + PI/2.0
if ray_cast_2d.is_colliding() and ray_cast_2d.get_collider() == player:
player.kill()
func kill():
if dead:
return
dead = true
$Deathsound.play()
$Graphics/Dead.show()
$Graphics/alive.hide()
$CanvasLayer/DeathScreen.show()
$CollisionShape2D.disabled = true
z_index = -1
Im a complete novice when it comes to coding with GDscript, and I have a small amount of knowledge in python.
When I run it there is a small yellow arrow pointing to this line of code:
var dir_to_player = global_position.direction_to(player.global_position)
its also feeding me this error message:
Invalid get index 'global_position' (on base: 'null instance')