Search Unity

[SOLVED] Enemy Projectiles Not Damaging Player

Discussion in 'Scripting' started by ChristodoulosL, Mar 22, 2019.

  1. ChristodoulosL

    ChristodoulosL

    Joined:
    Mar 13, 2019
    Posts:
    6
    [Edit]: Solved! The projectiles were travelling underneath the player, so I re-positioned them and now it's all working.
    Hello everyone!
    I've got a problem with the game that I am creating, as the enemy's projectiles go straight through the player without dealing any damage. I'm not sure what the problem is, especially because it's basically the same script as my player's projectile script which is working fine. Appreciate any help!

    The enemy's weapon script:
    Code (CSharp):
    1. public class EnemyWeaponScript : MonoBehaviour
    2. {
    3.     //[SerializeField]
    4.     public GameObject CannonballSphere5;
    5.  
    6.     public float fireRate = 1f;
    7.     public float nextFire;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     public void Update()
    17.     {
    18.         CheckIfTimeToFire();
    19.     }
    20.  
    21.     public void CheckIfTimeToFire()
    22.     {
    23.         if (Time.time > nextFire)
    24.         {
    25.             Instantiate(CannonballSphere5, transform.position, transform.rotation);
    26.             nextFire = Time.time + fireRate;
    27.         }
    28.     }
    29. }
    The enemy projectile's script:
    Code (CSharp):
    1. public class ProjectileEnemy2 : MonoBehaviour
    2. {
    3.     public float lifeTime = 3.0f;
    4.  
    5.     public float moveSpeed = 40.0f;
    6.  
    7.     public float damage = 100.0f;
    8.  
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.         Destroy(this.gameObject, lifeTime);
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         Movement();
    19.     }
    20.  
    21.     private void Movement()
    22.     {
    23.         transform.position += Time.deltaTime * moveSpeed * transform.forward;
    24.     }
    25.  
    26.     void OnTriggerEnter(Collider other)
    27.     {
    28.         if (other.transform.tag == "Player")
    29.         {
    30.             other.GetComponent<Player>().takeDamage(damage);
    31.             Destroy(this.gameObject);
    32.         }
    33.     }
    34. }
     
    Last edited: Mar 22, 2019
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you debug other colliders name to see if it is hitting it at all? Is the projectile having a collider on it?
     
  3. ChristodoulosL

    ChristodoulosL

    Joined:
    Mar 13, 2019
    Posts:
    6
    So turn out the projectiles from the enemy were going underneath the player... I'm not sure how I missed that. Thanks anyway, appreciate the reply!