When I make a prefab of my enemy and put it in a spawner my enemy stops folowing the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform player;
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Vector2 movement;
void Start(){
rb = this.GetComponent<Rigidbody2D>();
}
void Update()
{
Vector2 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement = direction;
}
private void FixedUpdate() {
moveCharacter(movement);
}
void moveCharacter(Vector2 direction){
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
}
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.tag.Equals ("Bullet"))
{
Destroy (gameObject);
}
}
}
5069141–498281–Enemy.cs (913 Bytes)
who is the player ask the enemies
//give the player tag "Player"
GameObject player;
void Start () {
player = GameObject.FindWithTag ("Player");
}
void Update () {
if (player != null) {
//do something
}
}
I can’t get it to work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float moveSpeed = 5f;
private Rigidbody2D rb;
private Vector2 movement;
GameObject player;
void Start(){
player = GameObject.FindWithTag("Player");
}
void Update()
{
if (player != null)
{
Vector2 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement = direction;
}
}
private void FixedUpdate() {
moveCharacter(movement);
}
void moveCharacter(Vector2 direction){
rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
}
}
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.
You need to create an else.
if (player != null)
{
Vector2 direction = player.position - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
rb.rotation = angle;
direction.Normalize();
movement = direction;
Debug.Log("My Player is NOT Null");
}
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:
else
{
Debug.Log("My Player is Null FML!!!");
player = GameObject.FindWithTag("Player");
}
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:
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.
try in line 23: player.**transform.**position
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.
Did it guys ty