Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to kill enemy AI.

Discussion in 'Scripting' started by cristo, May 28, 2015.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi, I'm firing projectiles at an enemy AI that's floating slightly above the player. The enemy is unresponsive to the projectiles.
    I don't know why.
    On the enemy I have this code:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Health = 50;
    4. var TheDammage : int;
    5. var deathparticles : GameObject;
    6.  
    7. function ApplyDammage (TheDammage : int)
    8. {
    9.     Health -= TheDammage;
    10.  
    11.     if(Health <= 0)
    12.     {
    13.         Dead();
    14.     }
    15. }
    16.  
    17. function Dead()
    18. {
    19.     //Destroy (gameObject);
    20.     //GameObject.SetActive(false);
    21.     gameObject.active = false;
    22.     var instance : GameObject = Instantiate(deathparticles, transform.position, transform.rotation);
    23.     Destroy (instance, 3);
    24.  
    25. }
    And I also have this code for AI:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class enemyAIScript : MonoBehaviour {
    5.  
    6.     public Transform player;
    7.     public float playerDistance;
    8.     public float rotationDamping;
    9.     public float moveSpeed;
    10.     public static bool isPlayerAlive = true;
    11.     //public float posY;
    12.  
    13.     public GameObject BirdM;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.  
    19.     }
    20.     void Awake ()
    21.     {
    22.         //posY = transform.position.y;
    23.         //playerDistance = Vector3.Distance (player.position, transform.position);
    24.      
    25.         //if (playerDistance < 180f)
    26.         //{
    27.         //    enemy.animation.Play ("dip");
    28.         //}
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.  
    34.    
    35.         //if (isPlayerAlive) {
    36.             playerDistance = Vector3.Distance (player.position, transform.position);
    37.         if (
    38.             playerDistance > 120f)
    39.             BirdM.animation.Play ("idle");
    40.  
    41.             if (playerDistance < 120f)
    42.             {
    43.                 lookAtPlayer ();
    44.             }
    45.  
    46.             if (playerDistance < 90f) {
    47.                 if (playerDistance > 5f) {
    48.                     chase ();
    49.                 } else if (playerDistance < 5f) {
    50.                     attack ();
    51.                 }
    52.             }
    53.         }
    54.     //}
    55.  
    56.  
    57.     //void diveAtPlayer()
    58.     //{
    59.                 //float enemySpeed = 7.0f;
    60.                 //float totalSpeed = enemySpeed * Time.deltaTime;
    61.                 //transform.Translate (Vector3.down * totalSpeed);
    62.             //    enemy.transform.position =  new Vector3 (52, 4, -178);
    63.         //    enemy.transform.position.y = player.position.y + 2;
    64.  
    65.     //}
    66.  
    67.  
    68.  
    69.     void lookAtPlayer()
    70.     {
    71.         Quaternion rotation = Quaternion.LookRotation (player.position - transform.position);
    72.         transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationDamping);
    73.         //BirdM.animation.Play ("attack");
    74.     }
    75.     void chase ()
    76.     {
    77.      
    78.         transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    79.         {
    80.             BirdM.animation.Play ("attack");
    81.             }
    82.     }
    83.     void attack()
    84.     {
    85.         RaycastHit hit;
    86.         if(Physics.Raycast(transform.position, transform.forward, out hit))
    87.         {
    88.             if (hit.collider.gameObject.tag == "Player")
    89.             {
    90.              
    91.              
    92.                 //hit.collider.gameObject.GetComponent<healthScript>().health -= 5f;
    93.             }
    94.         }
    95.     }
    96. }
    97.  
    The gun script is this:

    Code (JavaScript):
    1. #pragma strict
    2. var theBullet : Rigidbody;
    3. var speed = 20;
    4.  
    5.  
    6. function Update () {
    7.  
    8. if ( Input.GetButton ("Fire1")) {
    9.  
    10. var clone = Instantiate(theBullet, transform.position, transform.rotation);
    11. clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
    12. Destroy (clone.gameObject, 6);
    13.  
    14. }
    15. }
    16.  
    And the damage in the projectile is this:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var Dammage = 100;
    4.  
    5.  
    6.  
    7. function OnCollisionEnter (info : Collision)
    8. {
    9.     info.transform.SendMessage("ApplyDammage", Dammage, SendMessageOptions.DontRequireReceiver);
    10.  
    11.  
    12. }
    13.  
    And there's a capsule collider in the enemy.

    Anyway, the enemy doesn't die. All the other enemies do die during the same scene, with the same enemy logic, gun script and bulletdammage script set up. I have no idea why this flying enemy isn't dying. The only different seems to be that it's slightly above the player. But I can't see anywhere in the code where it would be dependent on that for the code to function.

    If anyone knows why it doesn't work, that would be great!
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    does the flying enemy have a collider?

    fyi damage has 1 m...
     
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Yeah, it has a capsule collider. I've tried different types of colliders but I don't think it should make a difference.

    The two mms in damage came from following a youtube tutorial For some reason the person spelled damage like that. I think they had a reason for it, but I can't remember it now.

    The problem might have something to do with the import settings of the FBX enemy. Some of its textures are missing. Anyway, I'll keep messing around with it. I may do it over from the start again. Anyway, thanks for your time. :)

    QUOTE="LeftyRighty, post: 2133418, member: 147015"]does the flying enemy have a collider?

    fyi damage has 1 m...[/QUOTE]
     
  4. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    try inserting some debug.log statements to find out if the right parts of your code gets called
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    fbx import/textures/mesh shouldn't impact how the colliders work and react. In theory you could strip the enemy down to just a gameobject with it's collider and scripts and it should work the same as with all the renderer stuff.

    seem like it's quite a popular tutorial vid, we see quite a few newbies with double m damage on here lol :)