Search Unity

AI doesn't know what my player is

Discussion in 'Scripting' started by overseer314, Apr 15, 2015.

  1. overseer314

    overseer314

    Joined:
    Apr 15, 2015
    Posts:
    11
    I have made a simple script that spawns an enemy every few seconds, however, when spawned, the enemy doesn't have any idea what my player is, so it just wanders away. It's spawning a prefab of "enemy" so is there a way to set my player as its target right when it is spawned? or even better yet, just have my player be the target in the prefab? Thanks in advance.
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Code (CSharp):
    1. void Start(){
    2.     player=GameObject.Find("Player);
    3. }
    change "Player" to the name of the player object in the scene, and player to whatever your variable is
     
    overseer314 and hey_m8 like this.
  3. hey_m8

    hey_m8

    Joined:
    Apr 15, 2015
    Posts:
    10
    You can also search for the gameobject by tag which is what I prefer:

    Code (CSharp):
    1. private GameObject player;
    2. public string playerTag
    3.  
    4. private void Start()
    5. {
    6.    player = GameObject.FindGameObjectWithTag(string playerTag);
    7. }
    8.  
    9. private void Update()
    10. {
    11.     if(player != null)
    12.     {
    13.         dosomething;
    14.     }
    15. }
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or you can pass in a reference when you instantiate it.

    Or you can get a static reference to the player.

    Or you can use ray casts and line of sight to attempt to find the player.

    Plenty of ways to skin this cat.
     
  5. overseer314

    overseer314

    Joined:
    Apr 15, 2015
    Posts:
    11
    Code (JavaScript):
    1. var Health = 100;
    2. var Player : Transform;
    3. var MoveSpeed : int = 5;
    4. Player = GameObject.Find("Player");
    5.  
    6.  
    7. function Update ()
    8. {
    9.     if (Health <= 0)
    10.     {
    11.         Destroy(gameObject);
    12.     }
    13.    
    14.     transform.LookAt(Player);
    15.     transform.position += transform.forward * MoveSpeed * Time.deltaTime;
    16. }
    I have my player as a transform. I tried changing it to a game object, but then the transform.LookAt(Player) doesn't work. is there a way to either make it something like GameObject.LookAt(Player) and so on, or can I do something like Transform.Find("Player");
     
  6. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Use the code I posted before, but just add .transform at the end.

    (GameObject.Find("Player").transform instead of GameObject.Find("Player"))
     
  7. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    Also, I don't know much about Javascript in unity, but move the "Player=...." line to inside of the Start() function so it works properly. Unless you don't have to do that and I'm mistaken?
     
    Kiwasi likes this.
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You have it right.
     
  9. overseer314

    overseer314

    Joined:
    Apr 15, 2015
    Posts:
    11
    Worked like a charm! now the enemies follow my player when spawned! thanks a ton!
     
  10. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    No problem.