Enemy AI not working correctly
Asked Answered
P

13

0

on this code i wanted to calculate the direction of where is the player and go against it but it doesn't work because it goes only to the right even if i am aproching the detector on the left side, can anyone help me pls?

`extends CharacterBody2D

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player
var chase = false
var SPEED = 50

func _physics_process(delta):
velocity.y += gravity * delta

if chase == true: 
	get_node("AnimatedSprite2D").play("Jump")
	player = $"../../Player/player"
	var direction = (player.position - self.position).normalized() 	
	print (direction)
	if direction.x > 0:
		get_node("AnimatedSprite2D").flip_h = true
	else:
		get_node("AnimatedSprite2D").flip_h = false
	velocity.x = direction.x * SPEED
else:
	get_node("AnimatedSprite2D").play("Idle")
	velocity.x = 0
move_and_slide()	

func _on_player_detection_body_entered(body):
if body.name == "Player":
chase = true

func _on_player_detection_body_exited(body):
if body.name == "Player":
chase = false
`
https://clipchamp.com/watch/hVNFm67PIIT

Plush answered 7/11, 2023 at 18:49 Comment(0)
I
0

Plush I don't know how your scene is organized but try using the global position of the player and the enemy.

var direction = (player.global_position - self.global_position ).normalized()

If that doesn't work then you might need to show some screenshots, more code or show/describe how your scene is structured.

Iciness answered 7/11, 2023 at 21:57 Comment(0)
P
0

Iciness i edited the post maybe now is better

Plush answered 8/11, 2023 at 10:0 Comment(0)
R
0

Plush Johnstone's solution will likely fix it as as the nodes have parent's that will affect their position versus the global_position. Did you try this?

I can recreate it as posted and changing to global_position fixes it.

Rahm answered 8/11, 2023 at 12:20 Comment(0)
P
0

Rahm i have tried to use global_position but this is the result. i think i am doing something wrong in the hierarchy because i took this code from a tutorial because i am new to godot but after trying differents method i don't know what to do anymore
https://clipchamp.com/watch/gXt2rmk3wXa

Plush answered 8/11, 2023 at 13:34 Comment(0)
I
0

Plush Why do you have two player nodes in your scene, "Player" and "Player/player"?

You are getting the position of this one: player = $"../../Player/player"

Is that the player node that is actually moving?

Iciness answered 8/11, 2023 at 13:44 Comment(0)
R
0

Plush then there's something else wrong and worth verifying your code against the source material as that is unlikely to be incorrect (also, what tutorial?). Zip up and share your project here, I'm sure it just needs a fresh pair of eyes.

Rahm answered 8/11, 2023 at 14:0 Comment(0)
P
0

Iciness yes the player with the lowrercase p is the one i want to move, so i think i am getting the right player

Player is the mother of the other player as you can see on the picture

Plush answered 8/11, 2023 at 14:30 Comment(0)
P
0

Rahm the tutorial is

but my zip file is 11 mb so i cant share it i think because on the godot forum i cant upload more than 2 mb, if you know an another method pls help me, thanks

Plush answered 8/11, 2023 at 14:32 Comment(0)
I
1

Plush but my zip file is 11 mb so i cant share it

Make sure that you do not include the .godot folder. If that is still too big then you could make a simplified version by ripping all the assets and unnecessary scenes out of it because for finding the issue we don't need any fancy graphics or animations.

Iciness answered 8/11, 2023 at 14:37 Comment(0)
P
0

Iciness thanks for the advice

prova-3.zip
1MB
Plush answered 8/11, 2023 at 14:56 Comment(0)
I
0

Plush I am getting a lot of error messages.

Iciness answered 8/11, 2023 at 15:29 Comment(0)
I
2

Plush Alright, the problem is that you are not getting the position of the correct player node. Your code works if you change line 14 to:

player = $"../../Player/player/Player"

The problem is your weird player node setup. You have "Player/player" in World which is just a Node2D.

And your player scene looks like this:

Which is the same Node2D from the World scene at the top and below it is the actual CharacterBody2D "Player". Unless you have very good reasons to do it like this you should restructure your scenes so that the root node of your Player scene is a CharacterBody2D and you should also remove the first "Player" Node2D from World.

There should only be one node called Player and it should be a CharacterBody2D. Basically you want your World scene to look like this:

Iciness answered 8/11, 2023 at 15:41 Comment(0)
P
0

Iciness Thanks omg!! It worked, and i learned new things, thank you very much again, you are very kind! 🙂

Plush answered 8/11, 2023 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.