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 ();
}
`
what object is getting clicked; is it one that is in the way, or a parent, or something way off?
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
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.
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!
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.
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
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.
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?
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)?
Your answer
Follow this Question
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