Anton-Stechman / rpg_mania_iv_submission

Creative Commons Zero v1.0 Universal
1 stars 0 forks source link

Unity C# Script: NPC AI #10

Open Anton-Stechman opened 1 year ago

Anton-Stechman commented 1 year ago

This ticket involves implementing a simple NPC script in Unity using the Navmesh Agent. The script should be designed to be used for both friendly and enemy NPCs. The NPC script should include basic functionality such as movement, targeting, and attacking.

The script should use the Navmesh Agent to navigate the game world and be able to detect and avoid obstacles. Additionally, the NPC should be able to detect and track the player character or other targets, and use appropriate attack behaviors based on its type (friendly or enemy).

The implementation should be modular and well-organized, with clear and concise code. The NPC script should be designed with future expansion in mind, so that additional behaviors and functionality can be added as needed.

Once implemented, the NPC script should be thoroughly tested to ensure that it works as expected, and any issues or bugs should be addressed promptly.

nmhart322 commented 1 year ago

For scalability, consider making your own movement script that uses the path generated by the nav agent. The default Navmesh Agent is very resource hungry, so when you have a lot in a scene it can tank your fps. What I usually do is update the agent path after an increment of time, and then use basic movement to step through the points on the path.

Anton-Stechman commented 1 year ago

Thanks for the tip! If I decide to take this further than just for an itch.io game jam I'd definitely be looking at writing a custom Navigation script, for simplicity I had planned just to use the out of the box functionality. But, I do like the idea of having a middle ground and just using the path from the NavmeshAgent.

How does this work? is there some nodes you can hook into from the NavmeshAgent?

nmhart322 commented 1 year ago

https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.html

Here is the docs link to the navmesh agent. With AgentName.path you can assign the path to an array of tranforms. It returns a set of points calculated by the navmesh. Then you can step through the points using a loop. I can set up a simple example for you if you would like!

There is some cool stuff you can do, like setting up an array of transforms as a patrol route, and then set the path using the same method to make the agent follow your path. You can also use the navmesh agent as a "ghost object" to go in front of your npcs, then make a physics based object follow the position of the agent, effectively making a physics based npc that uses the out of the box Unity navmesh.

Edit: Yes I guess you can call them nodes, depending on how you use the default methods.

Anton-Stechman commented 1 year ago

https://docs.unity3d.com/ScriptReference/AI.NavMeshAgent.html

Here is the docs link to the navmesh agent. With AgentName.path you can assign the path to an array of tranforms. It returns a set of points calculated by the navmesh. Then you can step through the points using a loop. I can set up a simple example for you if you would like!

There is some cool stuff you can do, like setting up an array of transforms as a patrol route, and then set the path using the same method to make the agent follow your path. You can also use the navmesh agent as a "ghost object" to go in front of your npcs, then make a physics based object follow the position of the agent, effectively making a physics based npc that uses the out of the box Unity navmesh.

Edit: Yes I guess you can call them nodes, depending on how you use the default methods.

Awesome, I’ll look into it!

I like the idea of the ghost object, this would allow me to utilise the parent ‘Character’ class I’ve already setup for the player object (with a little editing)