Search Unity

Question A* enemy prefab

Discussion in 'Prefabs' started by flyfill, Sep 27, 2020.

  1. flyfill

    flyfill

    Joined:
    Sep 27, 2020
    Posts:
    1
    Hi i am new to Unity and i have a problem.

    I try to make a wavespawner that spawns enemies from a prefab. Those enemy prefabs should spawn and start follow the player. Its a 2D game and i am using Aron Granberg's Astar Pathfinder. I easily set up all scripts for my enemy prefab..

    but

    the only problem with the Destination script i can't solve is:
    How do i get the Transform from the Player into that prefab.

    If you know the answer please keep it simple thanks!
     
  2. StapledGrinz

    StapledGrinz

    Joined:
    Dec 10, 2020
    Posts:
    1
    I'm having the same issue...did you ever find a solution?

    I think the best way to fix it, is to add a start function that makes the enemy target the player at when it spawns, but I'm not sure how to do that since I'm a beginner to reading/writing code.
     
  3. J_s_c

    J_s_c

    Joined:
    Aug 4, 2021
    Posts:
    1
    Uhh.. I'm beginner as well. But there is this code : Vector3.MoveTowards. This code can be used to follow a player.
    Link : https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
    Note: target is Player GameObject

    Code (CSharp):
    1.  
    2. public float speed = 20;
    3. public GameObject target;
    4.  
    5. void Update(){
    6.        ChaseTarget(target);
    7. }
    8.  
    9. void ChaseTarget(GameObject target)
    10. {
    11.        // Move our position a step closer to the target.
    12.         float step = speed * Time.deltaTime; // calculate distance to move
    13.         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
    14. }
    15.  
    Not sure this is what you looking for. Hope this helps you.
     
    Last edited: Aug 12, 2021