Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

player's pet attack.

Discussion in 'Scripting' started by 2dfruity, May 15, 2014.

  1. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    have a script that spawns NPC's and uses the FindGameObjectWithTag reference to follow the player, now i need these NPC's to attack enemys. is there a way to look at another tag for a time while being able to come back to the original?

    these NPC's act like guard dogs.
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    You can store a reference to the player, and have a different reference for enemies. If there are any enemies around, attack them, but if not (or if the player moves too far), follow the player. You don't need to get rid of the player reference to do this.
     
  3. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    how would i go about doing this?
    how do you store a tag?
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You store the reference.

    You could it like this:
    Code (csharp):
    1.  
    2. private var player_hero : GameObject;
    3.  
    4. function Start()
    5. {
    6.     player_hero = GameObject.FindWithTag("Player");
    7. }
    Also, you don't want to search for tags the whole time. The only enemies that are relevant to the NPCs are the ones that are within their range. Just add a collider on them and check for tags on collision.
     
  5. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    how are you currently following the player?

    You shouldn't be calling FindGameObjectWithTag every frame - you should store the result in a GameObject Variable that you can reference later.

    There are certainly other/better ways, but for the sake of simplicity, an example:

    Code (csharp):
    1.  
    2. private GameObject playerObject;
    3.  
    4. void Start(){
    5.     playerObject = GameObject.FindWithTag("player");
    6. }
    7.  
    8. void Update (){
    9. //code to follow player, using the variable playerObject
    10.  
    11. }
    12.  
    13.  
    Try posting your code that the AI pet is using to follow the player around, and you can move from there to get it to chase down enemies.
     
  6. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    so button makes goul, goul normally follows player, but when enemy is close... go attack enemy. im almost there.


    using UnityEngine;
    using System.Collections;

    public class Goul : MonoBehaviour {
    public Transform target;
    public Transform player;
    public int moveSpeed;
    private Transform goul;
    public int idleDistance = 1;
    public int enemyDistance = 10;

    void Awake(){
    goul = transform;
    }

    void Start () {
    GameObject to = GameObject.FindGameObjectWithTag("Enemy");
    target = to.transform;
    GameObject go = GameObject.FindGameObjectWithTag("Player");
    player = go.transform;


    }

    void Update (){
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;

    }

    void FixedUpdate () {
    if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
    if (target.position.x < goul.position.x) {
    goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
    }
    else if (target.position.x > goul.position.x) {
    goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
    }
    }
    if (Vector3.Distance (goul.position, player.position) > idleDistance) {
    if (player.position.x < goul.position.x) {
    goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
    }
    else if (player.position.x > goul.position.x) {
    goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
    }
    }


    Debug.DrawLine(player.position, goul.position, Color.yellow);


    }

    }
     
  7. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    so i got them to follow the enemy after hitching a ride with the player, but when the enemy is destroyed it gives me an error.

    "object of type 'Transform' has been destroyed but you are still trying to access it"

    how do i do a check, or how do i ask it if there are any enemies left, and revert back to player as the follow target?

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Goul : MonoBehaviour {
    5.     public Transform target;
    6.     public Transform player;
    7.     public int moveSpeed;
    8.     private Transform goul;
    9.     public int idleDistance = 1;
    10.     public int enemyDistance = 10;
    11.  
    12.     void Awake(){
    13.         goul = transform;
    14.     }
    15.    
    16.     void Start () {
    17.         GameObject to = GameObject.FindGameObjectWithTag("Enemy");
    18.         target = to.transform;
    19.         GameObject go = GameObject.FindGameObjectWithTag("Player");
    20.         player = go.transform;
    21.        
    22.        
    23.     }
    24.  
    25.     void Update (){
    26.             Vector3 theScale = transform.localScale;
    27.                 theScale.x *= -1;
    28.                 transform.localScale = theScale;
    29.  
    30.         }
    31.  
    32.     void FixedUpdate () {
    33.         if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
    34.                         if (target.position.x < goul.position.x) {
    35.                                 goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
    36.                         }
    37.                         else if (target.position.x > goul.position.x) {
    38.                                 goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
    39.                         }
    40.             }
    41.  
    42.         else if (Vector3.Distance (goul.position, player.position) > idleDistance) {
    43.                         if (player.position.x < goul.position.x) {
    44.                                 goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
    45.                         }
    46.                         else if (player.position.x > goul.position.x) {
    47.                                 goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
    48.                         }
    49.                 }
    50.  
    51.        
    52.         Debug.DrawLine(player.position, goul.position, Color.yellow);
    53.         Debug.DrawLine(target.position, goul.position, Color.green);
    54.    
    55.  
    56. }
    57.  
    58. }
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Try checking to see if the target is null. If it is, then don't try accessing its properties and just go for the player. You should also have some logic checking the player as well, as you will get the same error if your player dies.

    You can set up some sort of enemy manager class that keeps track of the number of enemies in the scene, When an enemy dies, notify that class, and you can reference the class with your goul to know how many enemies are left.
     
    Last edited: May 15, 2014
  9. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    how in the world would you check if something is null when its a parameter? o_O

    i was thinking of setting up a list of enemies, but how would i tell the script "enemies are gone bro, go back to player."
     
  10. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    i can't check right now, but i believe that
    Code (csharp):
    1. (target == null)
    should return true once the object is destroyed.

    If your target is null, you need to either find another target, or stop referencing it and just follow your player until you have a target again.
     
  11. 2dfruity

    2dfruity

    Joined:
    May 1, 2014
    Posts:
    75
    thank you