I’m trying to make a first person rpg style game like skyrim but not nearly as big and I don’t know how to make AIs, I don’t even know where to start. I know how to put my enemy models into unity but not make them preform their animations I had in Maya. I would like the enemy to walk up to the player and attack them with a sword. Any kind of help would be greatly appreciated.
2 Answers
2No offense but you might want to google a few tutorials or mess around with Unity’s demo projects, rather than posting that you don’t know how to do the most fundamental tasks such as importing a model.
Nevertheless, here are two links: one to the demo projects, which explain some stuff like assembling prefabs, and one to the first of a series of tutorials which I found really helpful myself.
Hey Hackerlab - see the problem is … AI is an incredible large topic. No one can and will give you an out of the box solution for your not really precise (
) question.
An incredible simplified approach would be to attach a sphere collider to the enemy - which acts as sort of a “perception” radius. As soon as the player enters the collider store the player in a target variable.
// create a variable to store the players game object
private GameObject target;
//unity specified function that takes a Collider as input
void OnTriggerEnter( Collider col ) {
// whatever just entered the trigger - does it have a tag that says "Player" - if so.. continue
if(col.transform.CompareTag("Player"){
// store the gameobject attached to the collider in target.
target = col.gameObject;
}
}
now you could simply use LookAt(target.transform.position) - to make your enemy always look at your player and Translate him along its forward direction using Transform.Translate (Vector3.forward) .
something like :
private float moveSpeed = 5f;
void Update () {
//if we have a target
if(target != null) {
// face the target
transform.LookAt(target.transform.position);
//walk towards the target (along the z coordinate) with a speed of moveSpeed per
//seconds
transform.Translate(Vector3.forward*Time.deltaTime*moveSpeed);
}
}
but this approach is terrible - however anything more complicated will definitely big too big for this forum.
I feel you , just like me btw. have more urgent issues to study in programming - more “basic” stuff
.
I strongly recommend you to not rush things by simply buying an AI - pack of the asset store. You will need to code at least a little bit in order to make things work the way you want to.
In order to learn more about AI - and general programming I recommend Petey’s Hack and Slash tutorial series on www.burgzergarcade.com
and especially for the AI Eteeski 's Youtube Channel.
Btw. their might be some typos in the code i provided - it simply works as an example.
with that code the enemy will ignore walls or any obstacles - if the player jumps the AI will rotate in weird ways and currently it won’t stop and attack the player… for the sake of it :
create a variable called
private float stopDistance = 2.5f; ( or whatever)
and add behind if we have a target command line → if(Vector3.Distance(transform.position, target.transform.position) >stopDistance)
now the AI should stop 2.5f units away from you.
(consider placing the LookAt() command somewhere else now)
to attack you would call some function Attack();
that you would have to specify… obviously you would want a healthscript on the player /AI as well… which is another function…
I hope you start to realize how much work you will be facing …
If you have some more precise questions I will gladly try my best to help you further. But please understand - you won’t make it without a lot of reading ![]()
Cheers Daniel
@debunked What a horrible, jack-as* way of answering this obviously delicate question.... No wonder no one wants to ask techies a question.... You get arrogant answers like THIS!!
– erik_malkavian* http://unitygems.com/ai-introduction/ * http://unity3d.com/learn/tutorials/projects/stealth/enemy-ai
– AlucardJay