Godot Version
Godot_v4.3-stable_win64.exe
Question
tldr: enemy goes towards player start position and follows input on player afterward
game is a 2D sidescroller platformer
Art student-not coder but i’ve been having fun learning on my own.
my professor gave us a code for enemy chasing behavior but i couldn’t get mine to work properly… the chasing T/F value is immediately T as soon as the game is ran (i’m assuming because it’s detecting the floor as a node) and goes towards the player’s original position, eventually slowing down when it gets near (i dont want that i need constant speed). and when it reaches where the player was it follows my inputs wherever the player is, albeit in a sliding motion, slowing down at the release of an input eventually coming to a stop.
I’m assuming this script works for everyone else in my class, except for me…?(i dont have friends i can’t ask them at all) but i did jump ahead of the rest of the class so my script and theirs(the professor’s) is kinda different.
what the node tree looks like
- CharacterBody2D3
- CollisionShape2D
- Sprite2D
- Area2D
- CollisionShape2D
extends CharacterBody2D
var chasing = false
var is_dead = false
@onready var playertarget = get_tree().get_first_node_in_group("Player") #?I changed it to this from @export var playertarget : CharacterBody2D BECAUSE ERROR OF 'POSITION' TYPE NIL STUFF
var speed = 500 # |
# |
func _ready(): # v
pass
#player_node = get_node(playertarget) as Node2D ?maybe this has something to do but it was already '#'-d when he gave the script to us
func start_chasing(target): #?i think these functions are useless.. but what are your opinions?
chasing = true
func stop_chasing():
chasing = false
func _physics_process(delta: float) -> void:
# Apply gravity if the enemy is dead and falling
if is_dead:
velocity.y += 1000 * delta # Adjust gravity strength if needed
# Normal chase behavior
elif chasing:
var direction = (playertarget.position - position).normalized()
direction.y = 0 # Keep the enemy on the ground ?what the frick is this? can't i just do get_gravity() like normal?
velocity = direction * speed
# Check if enemy is off the ground to apply additional gravity
if not is_on_floor() and not is_dead:
velocity += get_gravity() * delta * 3.0
move_and_slide()
func damage(): #dont mind this my coding is pure hot working garbage rn and another enemy that shoots has its bullets interacting with this node
pass
func _on_area_2d_body_entered(body: Node2D) -> void:
if body.name == "CharacterBody2D": #?i have a question.. when it refers to body.name does it mean like the name of the node type (like how you can rename nodes) or the type name type like CharacterBody2D,Area2D, etc
print("chasing")
chasing = true
func _on_area_2d_body_exited(body: Node2D) -> void:
if body.name == "CharacterBody2D":
chasing = false
print("stop chasing")