I'm new to Unity and I've made a game in which I can make the levels using prefabs quickly, but I found that making Enemies in prefabs isn't a good idea because they can't follow or interact with the player. What is the best way to make Enemies spawn as soon as the level generates without having to individually create every single one?
Player Code (or at least the instance variables without the methods):
public class PlayerPhys : MonoBehaviour
{
[SerializeField] private float m_JumpForce = 400f;
[Range(0, 1)] [SerializeField] private float m_CrouchSpeed = .36f;
[Range(0, .3f)] [SerializeField] private float m_MovementSmoothing = .05f;
[SerializeField] private bool m_AirControl = true;
[SerializeField] private LayerMask m_WhatIsGround;
[SerializeField] private Transform m_GroundCheck;
[SerializeField] private Transform m_CeilingCheck;
[SerializeField] private Collider2D m_CrouchDisableCollider;
const float k_GroundedRadius = .2f;
private bool m_Grounded;
const float k_CeilingRadius = .2f;
private Rigidbody2D m_Rigidbody2D;
private bool m_FacingRight = true;
private Vector3 m_Velocity = Vector3.zero;
[Header("Events")]
[Space]
public UnityEvent OnLandEvent;
[System.Serializable]
public class BoolEvent : UnityEvent<bool> { }
public BoolEvent OnCrouchEvent;
private bool m_wasCrouching = false;
private Animator m_Anim;
Transform playerGraphics;
Enemy prefab code I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAttack : MonoBehaviour
{
public int health = 1;
// Update is called once per frame
void Update()
{
// Destroy this if killed
if (health <= 0)
{
Destroy(gameObject);
}
}
// Take damage
public void TakeDamage(int damage)
{
health -= damage;
}
}
I used this Brackeys tutorial to create levels, and all I'm confused about is how I can make it so that the level creator can create enemies which can interact with the player. I wish he went over how he made prefabricated coins able to be picked up by the player