#Simplifying chase code to just make wandering enemy.

1 messages · Page 1 of 1 (latest)

inner scroll
#

I tried to make a simplified version of the code in this video https://youtu.be/_AheThiIiyg?si=FrbpcDoR0N-zKo7E so I can use it for enemies that just wander. I'm trying to make a bird enemy without gravity that just goes back and forth between two points in the air. When i try to launch the game with this code, I get "invalid access to property or key 'flip_h' on a base object of type 'null instance'."

Here is the entire code I have. Is there anything I need to add or cut to make this work?:

🎮 Discover how to create smarter enemy AI in Godot using RayCast! 🕹️

In this video, I'll demonstrate how to utilize RayCast in the Godot Engine, so your enemies can detect and chase the player. I will walk you through the process of adding RayCast to an existing enemy in your game and explain all the key components.

Whether you're a beginner ...

▶ Play video
#

For reference, here is the entire code from the video, not mine:

#

I tried to make a wandering enemy using an animation player to move the node back and forth across the screen, but when I tried to make multiples of those it caused weird issues that broke my whole game, so I'm trying to just have the wander function be in the enemy's code

inner scroll
inner scroll
#

UPDATE: I simplified it to one raycast, but now I'm not sure what to do to get the enemy to move. When I remove the handle_movement function, my game runs but the enemy just sits there.

#

I'm guessing I need to tweak the code within the handle_movement function to make him work?

inner scroll
#

I'm still stuck on this 🤔

foggy thistle
#

move_toward() takes the target value and by how much you want to change towards that value. You need 2 arguments.
If you are still having your flip_h error. It means the animated_sprite variable is not being set to a valid node, most likely because the path is incorrect.

inner scroll
#

No problems with flip_h.
How should I go about adding the second argument? Right now I only have the SPEED variable to add to it, is there something else I can add that won't make my enemy do things I don't want it to do?

foggy thistle
#

do things you don't want it to?

inner scroll
#

or maybe is there a whole other method I should use under handle_movement?

foggy thistle
#

Arguments are separated by a single comma ,

inner scroll
foggy thistle
#

Generally you would just have the 2 points defined in code.
Then use move_toward() to reach one, and once close enough, start moving to the other. The syntax would be similar to this:

position = position.move_toward(target_position, speed)

inner scroll
#

in my case would target_position be my left_bounds/right_bounds variables?

foggy thistle
#

yeah, just wherever you want to go.

#

You can either set the target_position to the point you want to go to rn. Or use an IF to change what version of move_toward you use.

inner scroll
#

I probably did this wrong. The character doesn't move with this setup. Second image is trying to use bool.
Sorry I'm trying to remember exactly what a lot of this code even does, been stuck on this for a couple days.

foggy thistle
#

move_toward simply takes the Vector2 and makes its values approach the target, by the amount of the second parameter.
What error are you getting there?

#

Ah, i see it. You have an IF, but no condition for it.

inner scroll
#

yeah is there a condition I should add or not use if?

foggy thistle
#

I mean, an IF statement means you want it to happen under a specific condition.

#

I do not know what that condition you want that to be

inner scroll
#

I just want the character to move back and forth no matter what, so not really anything

foggy thistle
#

The character does not understand the term "just move back and forth"
You must define conditions for what to do at any given moment.
Like "if too close to point 2, move toward point 1"

inner scroll
#

What's the simplest way to achieve that? Also I was wrong about the flip_h, he's facing the wrong direction when he moves but I'd like to figure out the movement before I figure out the sprite direction

foggy thistle
#

Personally, (and how most do it) is by changing the current target variable. And when close enough to the target, change to the next target on an Array

But this is something a bit too complex for a begginer, since i noticed you haven't done any programming courses.

I can't really provide much more help withouy basically doing a course trough discord.

inner scroll
#

Good to know. 👍
Strangely, I have another enemy who uses the more completed version of the code from that video, and he functions pretty much exactly the way I want the vulture enemy to function, however there's all this extra chase player code that doesn't even work...

#

So technically I could copy the dino_bad script to the vulture and he'd work fine, I just don't like having all this leftover code that doesn't do anything. I guess I just need to take more courses, as you said, so I know what to use for these simpler tasks

foggy thistle
#

Once you know how it works. You may not even need a tutorial.
But what i am suggesting here is a programming course, not a Godot one.

Knowing the language and its syntax is not going to help much without knowing how to use it.

#

It will also help you understand the language more easily. So it is a win-win.