- Home /
 
Need Help with 2D raycast
EDIT: After taking a closer look at my code and game in general, I found that the I can reduce the health of the enemy, however something really weird is going on. When I shoot the enemies, the health of the enemies that are in the scene does not go down, however if I were to check on the actual enemy prefab in the prefabs folder, the health is going down. This is something I have never seen before and I have no idea of how actually fixing it.
Hello everyone. I am working in a 2D space shooter, so my idea is to have 2D sprites alongside some 3D models as enemies. I have arranged player moving and some AI behavior, like turrets rotating towards the player and evil cubes moving towards the player. I have also managed to get the 3d evil cubes destroyed on contact with my lasers using raycasting, however I am stuck trying to do the same for my 2D objects.
You see, I managed to make the lasers hit the 2d enemy sprites, but I don't know how to make it die. At first I thought about broadcasting a message since that is how I do it for the 3D ones, but this seems not to work with raycast 2D since there is no out parameter like in the regular ray cast. I will post what I have, so that I can be a clear as possible.
 using UnityEngine;
 using System.Collections;
 
 public class bulletRemove : MonoBehaviour {
 
     private Transform laserTransform;
     private enemyCube theEnemy;
     private enemyPlane enemyTwoD;
 
 
     public float floatHeight;
     public float liftForce;
     public float damping;
 
 
     public float laserSpeed = 50.0f;
     public float bulletDamage = 25.0f;
     
     public GameObject enemyObject;
     public GameObject tank;
     public GameObject laserObject;
     // Use this for initialization
     void Start () 
     {
         //tank = GameObject.FindWithTag("enemyTwoD");
         Destroy (gameObject, 2.5f); //remove the laser after 2.5 seconds
         laserTransform = transform; //initialize the transform
         theEnemy = enemyObject.gameObject.GetComponent<enemyCube>();//get the enemyCube component so that we can see it later on
         enemyTwoD = tank.gameObject.GetComponent<enemyPlane>();
     }
     
     // Update is called once per frame
     void Update () 
     {
         laserCollision();
     }
 
     //define the laser collision function
     void laserCollision()
     {
         //shoot enemies using raycast
         RaycastHit hit;
         
         //create a ray of the size of the laser
         Ray bulletRay = new Ray(transform.position, laserObject.transform.up);
 
         Debug.DrawRay(transform.position, laserObject.transform.up); //debuging purposes
         Debug.DrawLine(transform.position, transform.up);//debuging purposes
         //Move forward
         laserTransform.Translate(Vector3.up * laserSpeed * Time.deltaTime);
        
         
         //enemy hit by the raycast then
         if (Physics.Raycast(bulletRay, out hit, 8.0f)) //Here I can use the out parameter
         {
             GameObject enemy = hit.collider.gameObject;//The out parameter being used
             
             //Send a message to the enemy object that is being hit and then apply damage to it
             enemy.BroadcastMessage("applyDamage", bulletDamage);//for some reason I am able to bradcast this massage to the enemy, therefore it works like a charm.
             
             Debug.Log("PEW, PEW, PEW... DIE EVIL CUBE FROM HELL!");
             //destroy the bullets on contact with the enemy
             enemyCube.Destroy(gameObject);//The 3D enemies are destroyed properly
             
         }
         //RaycastHit2D planeHit; thought I could work out something similar in 2D
         if(Physics2D.Raycast(transform.position, laserObject.transform.up, 3.0f))//However it is really different.
         {
             enemyTwoD.gameObject.BroadcastMessage("getDamage", bulletDamage);//When I try this I get an exception that says that message has no receiver.
             //enemyTwoD.getDamage(bulletDamage); I also tried to call the getDamage function from the object, but to no avail because the enemy does not want to die.
             Debug.Log("We have hit the enemy tank");//This confirms that my code is working, also the bullets get destroyed on collision, but the enemy will not die.
             
             enemyPlane.Destroy(gameObject);
         }
 
     }
 
     
 }
 
 
               Just in case I will also post the 2D enemy script
 using UnityEngine;
 using System.Collections;
 
 public class enemyPlane : MonoBehaviour 
 {
 
     public Transform target;
     public float health = 30.0f;
 
 
 
     private Transform myTransform;
     private float rotSpeed = 10.0f;
     private float angle;
     private Vector3 object_pos;
     private Vector3 target_Pos;
 
     
     // Use this for initialization
     void Start () 
     {
         myTransform = transform;
         target = GameObject.FindWithTag("Player").transform;
     }
     
     
     void Update()
     {
         death();
     }
     void FixedUpdate () 
     {
         lookAtPlayer();
     }
 
     //Create a function to make the enemy look towards the player
     void lookAtPlayer()
     {
         target_Pos = target.position;
         target_Pos.z = 0.0f;
         object_pos = myTransform.position;
         target_Pos.x = target.position.x - object_pos.x;
         target_Pos.y = target.position.y - object_pos.y;
         angle = Mathf.Atan2(target_Pos.y, target_Pos.x) * Mathf.Rad2Deg - 90;
         Vector3 rotationVector = new Vector3(0, 0, angle);
         myTransform.rotation = Quaternion.Euler(rotationVector);
     }
 
     //function to apply damage to the enemy
     public void getDamage(float damage)
     {
         health = health - damage;
     }
     
     //function to kill the enemy
     void death()
     {
         if(health <= 0)
         {
             Destroy(gameObject);
         }
     }
 }
 
 
               Thanks in advance for the help.
Your answer
 
             Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Using Raycasting For Enemies Attacking Player Objects 1 Answer
Difference between Unity's messaging system and other custom messaging systems? 1 Answer
In which script(s) are the messages sent by the Network class actually called? 0 Answers
ray cast not working ? 2 Answers