So I have this Unithealth Script, projectile script, and an Enemy script. I’m attempting to use the projectile script to deal damage to the enemy who has an object instantiated from the UnitHealth script. My problem is I can’t figure out the correct “language” to put on the projectile script in order to make this happen. Ideally the game will have multiple projectiles and different types of enemies. I feel like I’m close to it just can’t quite figure it out. I’ve tried to call the “Dmg Unit” from the projectile script but can’t seem to access it. Any help would be appreciated!
UnitHealth Script
public class UnitHealth
{
//Fields
private int _currentHealth;
private int _currentMaxHealth;
// Properties
public int Health
{
get { return _currentHealth; } set { _currentHealth = value; }
}
public int MaxHealth
{
get { return _currentMaxHealth; }
set { _currentMaxHealth = value; }
}
//Constructor
public UnitHealth(int health, int maxHealth)
{
_currentHealth = health;
_currentMaxHealth = maxHealth;
}
// Methods
public void DmgUnit(int dmgAmount)
{
if (_currentHealth > 0)
{
_currentHealth -= dmgAmount;
}
}
public void HealUnit(int healAmount)
{
if (_currentHealth < _currentMaxHealth)
{
_currentHealth += healAmount;
}
if (_currentHealth > _currentMaxHealth)
{
_currentHealth = _currentMaxHealth;
}
}
}
Enemy Script
public class EnemyAI : MonoBehaviour
{
public UnitHealth _basicEnemy = new UnitHealth(40, 40);
public NavMeshAgent agent;
public Transform player;
public Transform bulletSpawn;
public LayerMask whatIsGround, whatIsPlayer;
//Patroling
public Vector3 walkPoint;
bool walkPointSet;
public float walkPointRange;
//Attacking
public float timeBetweenAttacks;
bool alreadyAttacked;
public GameObject projectile;
public float rotationSpeed;
public float chaseAttackDelay;
//States
public float sightRange, attackRange;
public bool playerInSightRange, playerInAttackRange;
private void Awake()
{
player = GameObject.Find("Player").transform;
agent = GetComponent<NavMeshAgent>();
}
private void Update()
{
//Check for sight and attack range
playerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer);
playerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer);
if (!playerInSightRange && !playerInAttackRange) Patrolling();
if (playerInSightRange && !playerInAttackRange) ChasePlayer();
if (playerInAttackRange && playerInSightRange) AttackPlayer();
}
private void TakeDmg(int dmg)
{
_basicEnemy.DmgUnit(dmg);
}
private void Patrolling()
{
if (!walkPointSet) SearchWalkPoint();
if (walkPointSet)
agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
//Walkpoint reached
if (distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void SearchWalkPoint()
{
//Calculate random point in range
float randomZ = Random.Range(-walkPointRange, walkPointRange);
float randomX = Random.Range(-walkPointRange, walkPointRange);
walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);
if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
walkPointSet = true;
}
private void ChasePlayer()
{
agent.SetDestination(player.transform.position);
}
private void AttackPlayer()
{
//Make sure enemy doesn't move
agent.SetDestination(transform.position);
transform.LookAt(player);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
if (!alreadyAttacked)
{
///Attack code here
Rigidbody rb = Instantiate(projectile, bulletSpawn.position, Quaternion.identity).GetComponent<Rigidbody>();
rb.AddForce(transform.forward * 32f, ForceMode.Impulse);
///End of attack code
alreadyAttacked = true;
Invoke(nameof(ResetAttack), timeBetweenAttacks);
}
}
private void ResetAttack()
{
alreadyAttacked = false;
}
private void DestroyEnemy()
{
Destroy(gameObject);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, attackRange);
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(transform.position, sightRange);
}
}
Projectile Script
public class DefaultProjectile : MonoBehaviour
{
public float speed;
public float despawnTime = 2;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
Destroy(gameObject, despawnTime);
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Hit");
Destroy(gameObject);
}
}