Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] Script not triggering when projectile collides with enemy.

Discussion in 'Scripting' started by Deleted User, Aug 12, 2014.

  1. Deleted User

    Deleted User

    Guest

    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:

    Screen Shot 2014-08-12 at 1.19.39 PM.png Screen Shot 2014-08-12 at 1.19.20 PM.png

    This is my projectile script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ProjectileScript : MonoBehaviour
    5. {
    6.  
    7.         public float speed;
    8.         public int damage;
    9.         public float lifeSpan;
    10.         public bool isEnemyShot;
    11.         public Vector3 targetPosition;
    12.  
    13.         void Update ()
    14.         {
    15.                 transform.position = Vector2.MoveTowards (transform.position, targetPosition, speed * Time.deltaTime);
    16.         }
    17. }
    Enemy Health script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HealthScript : MonoBehaviour
    5. {
    6.  
    7.         public int hp = 1;
    8.         public bool isEnemy = true;
    9.  
    10.         public void Damage (int damage)
    11.         {
    12.                 hp -= damage;
    13.  
    14.                 if (hp <= 0) {
    15.                         Destroy (gameObject);
    16.                 }
    17.         }
    18.  
    19.         void OnTriggerEnter2D (Collider2D otherCollider)
    20.         {
    21.                 print ("shshs");
    22.                 // Is this a shot?
    23.                 ProjectileScript projectile = otherCollider.gameObject.GetComponent<ProjectileScript> ();
    24.                 if (projectile != null) {
    25.                         print ("runs here");
    26.                         // Avoid friendly fire
    27.                         if (projectile.isEnemyShot != isEnemy) {
    28.                                 Damage (projectile.damage);
    29.                                 print ("runs here2");
    30.                                 // Destroy the shot
    31.                                 Destroy (projectile.gameObject); // Remember to always target the game object, otherwise you will just remove the script
    32.                         }
    33.                 }
    34.         }
    35. }
     
  2. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    The projectile passes through the object because it's going way too fast.
    People use raycast to prevent this problem.
     
  3. Deleted User

    Deleted User

    Guest

    Even when the speed is 2 it still moves right through!
    What function(s) could I use to fix this?
     
  4. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    Well, it passes through cause the collider is a trigger.
     
  5. Deleted User

    Deleted User

    Guest

    Yes, I know but it's not supposed to pass through.
    The "HealthScript" is supposed to delete the projectile upon collision, but no collision is registered.
    Reading the MoveTowards API, I don't believe the problem is speed related.
     
  6. Deleted User

    Deleted User

    Guest

    Fixed it! It's because I used "EnterCollision2D" but the sphere is 3D.
     
    Magiichan likes this.