Advanced AI C# (Follow & Attack)

So I want to make a advanced AI monster that will basically follow you and once close enough stop to attack you.

Basically I would like to know what is the best way to go about making a monster that at first will stand still, then once the player gets closer it plays an animation and it remains standing still BUT starts rotating towards you, always trying to face you. Then once you get even closer it will stop doing the other animation and start doing a walk animation & move towards you. If you turn around it will charge you (preferbly a one time action) by increasing its speed and changing to a run animation. Once it is near you it will stop moving and attack you (dealing damage + animation).

Ive tried my best to search through alot of things but most things are just “simple AI” that is just too simple for this, and then there is the official unity tutorial which is wayy too advanced for this.

I have all my models and animations self made so all I have to do is script it.

So any suggestions, ideas or references to help me understand how to script this are welcome.

Or if you are willing you can create a basic template that might be like what i want.

Thank you for reading.

There’s two options here:

  1. Get someone to write you pretty much the entire thing so you can continue with your game, only to come around with a new issue the next day…

Or 2. Suggest ways for you to get the hang of programming yourself.

The second one seems wiser to me. In order to write the AI you want to write - I’m sorry, but really - you’ll have to start with lame simple AI. If AI at all. I’ll give you some leads for the AI, but I won’t tell you how to do it exactly.

Track distance between the player and the AI using Vector3.Distance, if that distance then drops below a set variable (let’s call it f1), you set a boolean true - also in an attached animator component using GetComponent().SetBool(), and then if the bool is true run a part of the script which sets the destination (using integrated NavMesh components) of the ‘monster’ to the player’s location.

I’m guessing you don’t know how to do any (or most) of this – don’t get me wrong, I’m not trying to burn you, but I’m trying to make you realize that having people write stuff for you and not learning yourself will only bring pain in the long run.

Follow tutorials, get the hang of coding and you’ll be able to write the complex AI from the logic you’ve got in your head. If you need something, come up with it and write it, don’t keep trying to get it from another place.

You’ll get there if you keep trying, and always remember…
Google is your friend! (Referring to the learning process, not this specific issue…)

@ArchoTek Thanks for the reply, Ill try to follow everything you said tomorrow since its late for me.

I certaintly have been doing alot of googling and mostly have figured things out myself, but AI is obviously a bit harder then what i have been working with, especially the multiple stage AI I am trying to achieve, though I will probably be happy enough if it alteast does half of the things I want it to.

Again, Thanks, Ill follow what you said and if i eventually reach a semi-solution Ill make this answer as the main solution.

If you want to write advanced AI, you might be interested to have a look at Behaviour Tree (BT).
It comes in handy for dealing with complex AI: because of its hierarchical nature, BT scales really well as the AI grows in complexity.

If you are looking for a BT framework, have a look at Panda BT. The package contains several examples, including an idle-chase-attack AI. I’m the author, so you can contact me directly if you have any question regarding this tool or about Behaviour Tree in general, I’d be glad to help.