Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alienguard · Jan 28, 2014 at 08:42 PM · raycastingbulletsenemiesmessage

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges