Tutorial 14 Enemy AI

Updated on January 29, 2016 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on January 29, 2016

Hi everyone,

after applying the scripts and using A* Pathfinder for my 2D top-down view shooter, I have a problem with the EnemyAI script made by Brackeys: I apply this script to a prefab (I made my enemy a prefab since I want to spawn waves of them), however, this prefab cannot take my Player’s tranform.
As soon as I add it to the scene, it is able to take its transform.. any ideas?

  • Liked by
Reply
3 on January 29, 2016

wait are u the one who asked for programmers etc at a big project where u wanted to rebuild a casual real life game ? (cuz i think i know the profile pictur 😀 and the question are similar to question which could come up at that project)

Helpful
on January 29, 2016

Mmn.. I don’t know what you are talking about so.. sorry to disappoint you, but I’m not that guy :/

Helpful
on January 29, 2016

Maybe make the player transform a variable and set it in the inspector then make it a prefab

Helpful
on January 29, 2016

Or put the code for getting player transform in the start or awake function

Show more replies
  • Liked by
Reply
Cancel
0 on January 29, 2016

If you want the AI to look for the player when they spawn you have 2 options.
1. Define the player game object in inspector by making a new public transform.
e.g. public Transform player; (Then you’d look in inspector and drag player object into this and use this transform for AI)
2. Have AI look for gameobject with the “Player” tag and then set that as the tansform to chase.
Alternatively you could look for the player objects name.

  • Liked by
Reply
Cancel
0 on January 29, 2016

Ok thx to both, will see if it works!

  • Liked by
Reply
Cancel
1 on January 29, 2016

OK SOLVED: I found out that the enemy being a prefab, the only way to make it keep track of the player position is through code:

public Vector2 target;

void Update(){

target = GameObject.Find (“Player”).transform.position;

}

that’s apparently because prefabs should be able to be used in any scene, also where the player might not necessairily be there, so that’s why Unity wasn’t allowing me to drag and drop the player onto the script.

Devoted
on January 29, 2016

I’m glad that it worked for you. 🙂

Show more replies
  • Liked by
Reply
Cancel