Search Unity

Enemy does not follow player anymore when from spawner

Discussion in '2D' started by JorisenTom, Oct 15, 2019.

  1. JorisenTom

    JorisenTom

    Joined:
    Jan 24, 2019
    Posts:
    15
    When I make a prefab of my enemy and put it in a spawner my enemy stops folowing the player
     
  2. JorisenTom

    JorisenTom

    Joined:
    Jan 24, 2019
    Posts:
    15
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public float moveSpeed = 5f;
    9.     private Rigidbody2D rb;
    10.     private Vector2 movement;
    11.  
    12.     void Start(){
    13.        rb = this.GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         Vector2 direction = player.position - transform.position;
    19.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    20.         rb.rotation = angle;
    21.         direction.Normalize();
    22.         movement = direction;
    23.     }
    24.     private void FixedUpdate() {
    25.             moveCharacter(movement);
    26.     }
    27.     void moveCharacter(Vector2 direction){
    28.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    29.     }
    30.     void OnCollisionEnter2D (Collision2D col)
    31.     {
    32.         if (col.gameObject.tag.Equals ("Bullet"))
    33.         {
    34.             Destroy (gameObject);
    35.         }
    36.     }
    37.    
    38.         }
    39.  
    40.  
    41.  
     

    Attached Files:

    • Enemy.cs
      File size:
      913 bytes
      Views:
      382
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    who is the player ask the enemies
    Code (CSharp):
    1. //give the player tag "Player"
    2. GameObject player;
    3.  
    4. void Start () {
    5. player = GameObject.FindWithTag ("Player");
    6. }
    7.  
    8. void Update () {
    9. if (player != null) {
    10. //do something
    11. }
    12. }
     
  4. JorisenTom

    JorisenTom

    Joined:
    Jan 24, 2019
    Posts:
    15
    I can't get it to work
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed = 5f;
    9.     private Rigidbody2D rb;
    10.     private Vector2 movement;
    11.     GameObject player;
    12.     void Start(){
    13.         player = GameObject.FindWithTag("Player");
    14.     }
    15.    
    16.  
    17.     void Update()
    18.     {
    19.         if (player != null)
    20.         {
    21.             Vector2 direction = player.position - transform.position;
    22.             float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    23.             rb.rotation = angle;
    24.             direction.Normalize();
    25.             movement = direction;
    26.         }
    27.     }
    28.     private void FixedUpdate() {
    29.             moveCharacter(movement);
    30.     }
    31.     void moveCharacter(Vector2 direction){
    32.         rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    33.     }  
    34. }
    35.  
     
  5. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Did you set the tag from the player to Player ?

    make 11. line
    public GameObject player;

    start the game, wait till the enemy is spawned. Pause the game, go to the scene tab, select the enemy and check in the inspector its field "Player". Is it empty or the enemy has found the player.
     
    MisterSkitz likes this.
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You need to create an else.
    Code (CSharp):
    1. if (player != null)
    2.         {
    3.             Vector2 direction = player.position - transform.position;
    4.             float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
    5.             rb.rotation = angle;
    6.             direction.Normalize();
    7.             movement = direction;
    8.             Debug.Log("My Player is NOT Null");
    9.         }
    10. else{Debug.Log("My Player is Null FML!!!");}
    We need to find out what the problem is. Let's start by seeing whether the player is getting found. If the player isn't found, then you want to run the code again:

    Code (CSharp):
    1. else
    2. {
    3. Debug.Log("My Player is Null FML!!!");
    4. player = GameObject.FindWithTag("Player");
    5. }
    Edit: I'm suspecting that your declaration of player is more of a var type than private. var takes on explicitly obvious initialization, so perhaps try:

    Code (CSharp):
    1. private GameObject player;
    But this may not be the real issue, I'm just spitballing here that possibly FindWithTag() isn't registering the datatype correctly.
     
    Last edited: Oct 15, 2019
    vakabaka likes this.
  7. JorisenTom

    JorisenTom

    Joined:
    Jan 24, 2019
    Posts:
    15
    this is what it is giving now
     

    Attached Files:

  8. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    try in line 23: player.transform.position
     
    MisterSkitz likes this.
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Since your Rigidbody2D is the declaration type, you must define the position but to do so, you require accessing the Rigidbody2D's transform position. You named the Rigidbody2D "player" so to properly access it's position, you need player.transform.position.

    Now if you declared Transform player then you would only need player.position because its type is a Transform so you do not need the transform because that is what it is already.
     
  10. JorisenTom

    JorisenTom

    Joined:
    Jan 24, 2019
    Posts:
    15
    Did it guys ty
     
    MisterSkitz likes this.