Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MantisMcKoy · Jun 04, 2016 at 03:41 AM · raycastcolliderlistsevents

Mouse click doesnt seem to be hitting the right collider

I'm new to using raycasts and I think everything in script is working as it should, except the object I click on is not the object that gets effected. Basically There is a Card Class and a Deck class and I want the user to click on one of the cards that are displayed and have it move from one Deck to another. I am guessing I did something wrong with either my Ray creation or my RaycastHit2D creation. Any Ideas?

 public delegate void ClickAction(Card card);
     public static event ClickAction OnClicked;
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction);
             Debug.Log (hit.point);
 
             if (hit.collider != null) {
                 Card cardScript = hit.collider.GetComponent<Card>();
                 if(cardScript){
                     Debug.Log("Got Card");
                     if(OnClicked != null){
                         OnClicked(cardScript);
                     }
                 }
             } else {
                 Debug.Log ("Didn't click game object");
             }
         }
     }

and if it matters the OnClicked event is ...

   void OnEnable(){
             ClickCard.OnClicked += MoveCardToBidHand;
         }
         void OnDisable(){
             ClickCard.OnClicked -= MoveCardToBidHand;
         }
         public void MoveCardToBidHand(Card card){
             Debug.Log ("Method was fired");
             bidHand.getList ().Add (card);        
             hand.getList ().Remove (card);
             hand.getList ().TrimExcess ();
         }

`

Comment
Add comment · Show 4
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 Alec-Slayden · Jun 04, 2016 at 11:23 AM 0
Share

what object is getting clicked; is it one that is in the way, or a parent, or something way off?

avatar image MantisMcKoy Alec-Slayden · Jun 04, 2016 at 08:51 PM 0
Share

there are only cards. I cant quite figure out a pattern but its always another card in the same list. So I have 6 cards splayed out and when I click the 3rd card it will act as if I clicked the first.

Edit: the more I mess around with it the more it tends to select the first card displayed. If i mouse over far enough to the right then it will move from the first card. That seems significant although I do not know what to make of it

avatar image flashframe · Jun 05, 2016 at 02:21 PM 0
Share

I worked around a similar issue by offsetting my gameobjects slightly in z (top to bottom) and then using Physics2D.GetRayIntersection (ins$$anonymous$$d of Physics2D.Raycast)

 RaycastHit2D hit = Physics2D.GetRayIntersection(ray, distance, layermask);

http://docs.unity3d.com/ScriptReference/Physics2D.GetRayIntersection.html

Not sure if this is suitable for your situation, but it works well for me.

avatar image MantisMcKoy flashframe · Jun 06, 2016 at 12:41 AM 0
Share

Im not sure If I understand this. This sounds like a similar solution to @skillbow 's solution. If I were to do this wouldn't i need to create a layer for each individual card?

EDIT: Flashfram you are completly correct! I did not understand that the ray I was using was a 3D ray!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by skillbow · Jun 04, 2016 at 10:49 AM

Try putting your card GameObject onto a layer and using a layer mask and also tagging your card as "Card"

 LayerMask layerMask = 1 << 8;  //8  is normally the first slot that you can add in layers, use whatever number layer your card is on
 
 
 if (Input.GetMouseButtonDown(0)) {
 
                 RaycastHit2D hitCheck = Physics2D.Raycast(_camera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, layerMask);

                             //make sure you're actually clicking on a collider
                 if(!hitCheck.collider)  
                     return;
                            //Check tag of gameObject for collider is "Card"
                 if(!hitCheck.collider.gameObject.CompareTag("Card")) 
                     return;
 
 etc...
 }

Hope this works.

Comment
Add comment · Show 6 · 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 MantisMcKoy · Jun 04, 2016 at 08:53 PM 0
Share

I'm not positive since im new to this but this sounds like a solution If the misclicks were hitting something that wasnt another card. So checking a tag for card alone wouldnt change anything but Ill look into the layer$$anonymous$$asking but i'm not sure if that is ideal

Edit: It seems to me, unless I am misunderstanding the purpose of layer $$anonymous$$asks, that that would help if I were clicking some other gameobject with the misclicks. What is happening Is I have 1 deck which displays 6 cards splayed out and when I click on one card it seems to 'get' another card. for some reason it seems to prefer to switch the first card in the list to the other list, although If i click sufficently far enough to the right (not even on another card just in blank space) it will act as if I clicked a card that was not the first card

avatar image skillbow · Jun 05, 2016 at 07:13 AM 0
Share

Ah ok, I think I might what's happening. Do you have this code attached to each card or the camera? the ray cast should be a script on the camera, if it's on each card it can get triggered for each card, rather than just the first card hit.

avatar image MantisMcKoy skillbow · Jun 05, 2016 at 02:22 PM 0
Share

The raycast script is attached to the camera. but The issue isnt that it gets triggered on every card, its that it almost always gets triggered on the first card. So when I click the first card it seems to work as it should, but If i click the second card then it only fires the first card. I added Debug Logs to show where Im clicking and it says im clicking in different places for example 1st card (0.1, 0.6) then when I click on the last card (1.3, .06) but in the second click it hits the first card again. I mostly mentioned the point coordinates because the difference sounds like it might be smaller than it should be?

avatar image skillbow MantisMcKoy · Jun 05, 2016 at 03:00 PM 0
Share

So are the cards stacked one on top of the other, or laid side by side? When the first card is clicked, it should move to another location (the other deck)?

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

66 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

Related Questions

Raycast not detecting? 0 Answers

How to get raycast to ignore objects behind a panel 0 Answers

Raycast + Collider + Mouse 0 Answers

How do you make one game object follow the shape of another game object? 0 Answers

Restrict held object movement 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