For some reason, the projectile just goes straight through the enemy and nothing occurs.
Pictures of the inspector for the projectile object and the enemy object:
This is my projectile script:
using UnityEngine;
using System.Collections;
public class ProjectileScript : MonoBehaviour
{
public float speed;
public int damage;
public float lifeSpan;
public bool isEnemyShot;
public Vector3 targetPosition;
void Update ()
{
transform.position = Vector2.MoveTowards (transform.position, targetPosition, speed * Time.deltaTime);
}
}
Enemy Health script:
using UnityEngine;
using System.Collections;
public class HealthScript : MonoBehaviour
{
public int hp = 1;
public bool isEnemy = true;
public void Damage (int damage)
{
hp -= damage;
if (hp <= 0) {
Destroy (gameObject);
}
}
void OnTriggerEnter2D (Collider2D otherCollider)
{
print ("shshs");
// Is this a shot?
ProjectileScript projectile = otherCollider.gameObject.GetComponent<ProjectileScript> ();
if (projectile != null) {
print ("runs here");
// Avoid friendly fire
if (projectile.isEnemyShot != isEnemy) {
Damage (projectile.damage);
print ("runs here2");
// Destroy the shot
Destroy (projectile.gameObject); // Remember to always target the game object, otherwise you will just remove the script
}
}
}
}