I’m editing an enemy that walks up and attacks, and making it shoot a projectile 3-D object instead, but I’m trying to find a way to have the collision between the player and bullet deal damage, but I just keep getting the same errors. Can you help me please?
Here’s my BulletDamage Script
public class BulletDamage : MonoBehaviour {
PlayerHealth playerHealth;
public int attackDamage = 40;
public GameObject player;
private void Start()
{
playerHealth = player.GetComponent<PlayerHealth>();
player = GameObject.FindGameObjectWithTag("Player");
}
private void OnTriggerEnter(Collider other)
{
if (other.tag=="Player")
{
playerHealth.TakeDamage(attackDamage);
}
}
}
and here’s my code for the attacking enemy
public class BossAttack : MonoBehaviour {
public float timeBetweenAttacks = 1f;
public int attackDamage = 40;
public GameObject shot;
public Transform shotSpawn;
public float fireRate = 0.5f;
private float lastShot = 0.0f;
bool playerInRange;
Animator anim;
public GameObject player;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
float timer;
void Awake()
{
playerHealth = player.GetComponent<PlayerHealth>();
enemyHealth = GetComponent<EnemyHealth>();
anim = GetComponent<Animator>();
}
void Update()
{
timer += Time.deltaTime;
if (timer >= timeBetweenAttacks && enemyHealth.currentHealth > 0)
{
Fire();
}
if (playerHealth.currentHealth <= 0)
{
anim.SetTrigger("PlayerDead");
}
}
void Attack()
{
timer = 0f;
if (playerHealth.currentHealth > 0)
{
playerHealth.TakeDamage(attackDamage);
}
}
void Fire()
{
timer = 0f;
if (Time.time > fireRate + lastShot)
{
var bullet=Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 40;
Destroy(bullet, 1.0f);
lastShot = Time.time;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
playerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject == player)
{
playerInRange = false;
}
}
}