Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RedMagma · Jul 23, 2015 at 06:10 PM · 2draycastraycasthit2d

2D Raycast effect only what it hits?

Hell again! I am having an issue with Raycast, the scripts is telling unity to "kill" all game objects with the tag "enemy" once a bool triggers to true. It triggers true once the Raycast collides with an enemy tagged game object.

But I need it so only the enemy the raycast is hitting will run the script, and be "killed". So how do I check to see if something is being hit by the raycast and apply that to only one of the prefabs, and not all of them.

This is the Raycast shooting script

     public Transform sightStart, sightEnd;
     public bool spotted = false;
 
     public bool isBeingHit = false;
 
     void Update() {
         Raycasting ();
         damage ();
     }
 
     void Raycasting() 
     {
         Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
         spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
     }
 
     void damage() {
         if(Input.GetKey (KeyCode.Mouse0)) {
             if(spotted == true) {
                 isBeingHit = true;
             }else {
                 isBeingHit = false;
             }
         }
     }

is works and once it sees a tagged enemy it triggers "spotted" to true. and once the player presses the mouse button "isBeingHit" also triggers to true, this triggers another script to kill the enemy.

 public playerShooting shoot;
     public Animator enemyAnim;
     public Collider2D col;
 
 
     void Start() {
         col = GetComponent<BoxCollider2D> ();
         enemyAnim = GetComponent<Animator> ();
         shoot = GameObject.FindGameObjectWithTag("player").GetComponent<playerShooting> ();
     }
 
     void Update() {
         if(shoot.GetComponent<playerShooting>().isBeingHit == true) {
             enemyAnim.SetBool ("isDead", true);
             col.enabled = false;
         }
     }

I have this on my enemy prefabs, that once trigged kills the enemy. But as expected it kills all of them even though they are not the one with the raycast on them. It is doing what it is being told to do by the script.

I just can't figure it out on how to make so only the one that is being hit by the raycast is the one to be killed, and not all of the prefabs on the map.

I hope this makes sense!

Thank for any 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by alexi123454 · Jul 23, 2015 at 07:05 PM

The reason it's killing all of them is because you put the "spotted" variable on the wrong object! When the raycast hits an enemy, instead of setting "spotted" to true on the player, each enemy object should have a "spotted" variable that is being set to true (if the player sees someone, the player isn't spotted, the ENEMY is).

Change the scripts to something like this:

  public Transform sightStart, sightEnd;
  
      public bool isBeingHit = false;
  
      void Update() {
          Raycasting ();
          damage ();
      }
  
      void Raycasting() 
      {
          Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
          RaycastHit2D hit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
          if (hit)
          {
              hit.transform.GetComponent<EnemyScriptName>().spotted = true;
          }
      }
  
      void damage() {
          if(Input.GetKey (KeyCode.Mouse0)) 
          {
              isBeingHit = true;
          }
          else
          {
              isBeingHit = false;
          }
      }

and this:

      public playerShooting shoot;
      public Animator enemyAnim;
      public Collider2D col;
      public bool spotted = false;
  
  
      void Start() {
          col = GetComponent<BoxCollider2D> ();
          enemyAnim = GetComponent<Animator> ();
          shoot = GameObject.FindGameObjectWithTag("player").GetComponent<playerShooting> ();
      }
  
      void Update() {
          
          if(shoot.GetComponent<playerShooting>().isBeingHit == true && spotted) 
          {
              enemyAnim.SetBool ("isDead", true);
              col.enabled = false;
          }
          else
          {
              spotted = false;
          }
      }
Comment
Add comment · Show 1 · Share
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
avatar image RedMagma · Jul 24, 2015 at 09:32 AM 0
Share

Thank you $$anonymous$$i! why did I not notice that, I was thinking it was going to be some complicated process. I have done it 100s of time in the past too. Thank you very much!

avatar image
1

Answer by nisovin · Jul 23, 2015 at 07:06 PM

It doesn't really make sense to have the isBeingHit variable on a component attached to the player object. Rather, it should be on a component attached to the enemy object.

Now, the Physics2D.Linecast method actually returns a RaycastHit2D object. You can treat it as a boolean, but you can also use it to get the collider of the object that was hit. So, assuming you've moved isBeingHit to your enemy component, and assuming your enemy component is called "enemy", then you can make your code look like this:

  public Transform sightStart, sightEnd;
  public RaycastHit2D spotted;
     
  void Update() {
      Raycasting ();
      damage ();
  }
 
  void Raycasting() 
  {
      Debug.DrawLine (sightStart.position, sightEnd.position, Color.red);
      spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer("enemy"));
  }
 
  void damage() {
      if(Input.GetKey (KeyCode.Mouse0)) {
          if(spotted != null) {
              spotted.collider.GetComponent<enemy>().isBeingHit = true;
          }
      }
  }

Now in your enemy script you can remove the reference to the player and just check its own isBeingHit variable.

      public bool isBeingHit;
      public Animator enemyAnim;
      public Collider2D col;
  
  
      void Start() {
          col = GetComponent<BoxCollider2D> ();
          enemyAnim = GetComponent<Animator> ();
      }
  
      void Update() {
          if(isBeingHit) {
              enemyAnim.SetBool ("isDead", true);
              col.enabled = false;
          }
      }

You could also probably do a bit of cleanup and code efficiency changes as well, like moving the Linecast into the damage() method, since you only need to do that if the mouse button is pressed. However, I've tried to leave your code as similar as possible to the way you had it.

Comment
Add comment · Show 1 · Share
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
avatar image RedMagma · Jul 24, 2015 at 09:33 AM 0
Share

Thank you Nisovin, don't know why I did not figure it out. I will take your advice and clean up like you said. Thank you

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Layer Mask on raycast2d not working? 1 Answer

Raycast for a 2D objects 0 Answers

Raycast2D will point right, but won't point left when player turns. 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

Error with 2D Raycast 0 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