#Need help making enemies that roam freely.

25 messages · Page 1 of 1 (latest)

surreal mulch
#

so I'm extremely new to the Godot engine and game development in general. I've followed two tutorials by the youtuber Brackeys, and since then I've been trying to learn the engine.

but anyways, I'm trying to make a 3D survival horror game. Right now I have a Whitebox level and a player character (prototype controller by Brackeys), next I wanna make a prototype enemy that will roam the level and chase the player when spotted. I've followed a tutorial using the NavigationRegion3D and NavigationAgent3D nodes, but with no success. how would I program a free roaming enemy that chases the player?

merry nebula
#

State machine would help you with everything I suppose

azure agate
#

What does "no success" mean? Does the enemy not even move?

surreal mulch
surreal mulch
merry nebula
#

I would recommend to watch so state machine videos and tutorials, these are industry standards for npcs and characters

azure agate
#

You don't need to think about state machines if it won't even move.

merry nebula
azure agate
#

Add a NavigationRegion3D with a nav mesh
Add a child MeshInstance3D. Set it as a large plane at walk height.
Click NavigationRegion3D and in the 3D window, click the bake button.

Give a RigidBody3D a NavigationAgentChild. Attach the following script to the RigidBody:


var speed = 5.0
var target =  Vector3(50, 0, -50) :
    set(value):
        target = value
        $NavigationAgent3D.target_position = value
func _ready():
    $NavigationAgent3D.target_position = target

func _physics_process(_delta):
    var pos : Vector3 = $NavigationAgent3D.get_next_path_position()
    linear_velocity = global_position.direction_to(pos) * speed

Then you just change the target var to change where the rigidbody is walking to. Spotting a player is a little more complicated, so make sure you can do this first.

surreal mulch
#

Change the target var to where it's walking to?

So like change the X, Y, Z axis in the vector3 code?

surreal mulch
#

I implement the code but I'm getting an error stating "no constructor of vector3 matches the signature". I haven't used vector3 in my project anywhere so it has no reference.

#

I assume

#

OH

#

I see

#

Yes it's working now

surreal mulch
merry nebula
surreal mulch
azure agate
#

target = PlayerNode.global_position in _physics_process. PlayerNode needs to be a reference to the player node, for testing you can just drag the actual node from the left window into the code. It'd look something like target = $"../CharacterBody3D".global_position

#

When not following the player, you don't need to update target too often.

azure agate
#

No. $"." is itself, the node you are currently writing a script for. You want the player body. You can put @export var player : Node above var speed, and then at the top of the inspector window, there will be a new option to assign the player node.
Then you just do var target = player.global_position

grizzled brook
#

What if you give the enemy a random point to go and make an area if the player enters that area the enemy follows him

#

You need to learn navigation agent and navigation region