Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Content1of_akind · Apr 21, 2019 at 11:11 PM · unity 2draycasthit2dclick objects

Two different Raycast 2D are hitting at the same time when i touch one of them

I create one raycast 2D to destroy the first gameObject on click and created a second raycast 2D to destroy the second gameObject on click.

When i click on the first gameObject both gameObjects are destroyed at the same time, why does that happen and How can I make it destroy only the object I touched?

 // First gameObject Script
 using UnityEngine.EventSystems;
 
 RaycastHit2D hit1Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
 RaycastHit2D hit1Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
         Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.green);
         Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.green);

         Ray firstRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
         {
 
             if (hit1Up.collider != null || hit1Bottom.collider != null)
             {
                 if (hit1Up.collider.tag == "TagName1" || hit1Bottom.collider.tag == "TagName1")
                 {
                     Debug.Log("You touched TagName1");
                     destroy(this.gameObject);
                 }
             }
             
         }
 
 
 // Second gameObject Script
 using UnityEngine.EventSystems;
 
 RaycastHit2D hit2Up = Physics2D.CircleCast(transform.position, 0.5f, Vector2.up * 0.35f);
 RaycastHit2D hit2Bottom = Physics2D.CircleCast(transform.position, 0.5f, Vector2.down * 0.77f);
         Debug.DrawRay(transform.position, Vector3.up * 0.35f, Color.yellow);
         Debug.DrawRay(transform.position, Vector3.down * 0.77f, Color.yellow);

         Ray secondRay = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
         {
 
             if (hit2Up.collider != null || hit2Bottom.collider != null)
             {
                 if (hit2Up.collider.tag == "TagName2" || hit2Bottom.collider.tag == "TagName2")
                 {
                     Debug.Log("You touched TagName2");
                     destroy(this.gameObject);
                 }
             }
             
         }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiegoSLTS · Apr 22, 2019 at 03:43 AM

You're not using the rays for anything, you're just creating them. The objects get deleted because you're doing CircleCasts at their position (which always result from a hit in both objects) and you delete them when you press the mouse button. Here's an example on how to detect clics on 2D objects using raycast: https://kylewbanks.com/blog/unity-2d-detecting-gameobject-clicks-using-raycasts

Comment
Add comment · Show 4 · 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 Content1of_akind · Apr 22, 2019 at 04:19 AM 0
Share

Thanks for the response but I tried the link you posted earlier today and it gave me the same result. I also need it to work when objects are moving fast. with the link you posted, after using it, I had to click the moving object multiple times because the its collider wasn't being detected while the object is moving quickly.

avatar image DiegoSLTS Content1of_akind · Apr 22, 2019 at 11:24 AM 0
Share

That doesn't sound right. Are you following the steps one by one? That script and your's have nothing in common, it's really weird that both have the same issue. You should just literally copy paste that code, check if it works, then make changes if you need them.

avatar image Content1of_akind DiegoSLTS · Apr 22, 2019 at 05:30 PM 0
Share

I copy and paste the code. Since the object I'm clicking has to be moving, the code is not working well because the object is moving faster than the object's collider attached to it. I tried two different RaycastHit2D but that doesn't work.

Show more comments

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

107 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 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 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 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 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 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

Raycasthit2d isnt working. 2 Answers

How to get co-ordinates of end point of Raycast2D ? 1 Answer

Raycast2D/Line Renderer offset on colliders based on mouse position 0 Answers

Unity 2D -Reflecting a linerenderer 2 Answers

Raycast Not Working properly 1 Answer


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