Search Unity

Finding GameObject under mouse click

Discussion in '2D' started by arncota, Nov 27, 2013.

  1. arncota

    arncota

    Joined:
    Nov 12, 2013
    Posts:
    14
    Hi all,

    I am building a simple card game. I have a Card prefab that I am laying out in my scene. I want to capture a left mouse click, and know which card I have clicked on. I have done alot of searching and tried a lot of things but I cant quite get it to work.

    I have a script attached to my Card prefab named CardPrefabScript. It has a public variable named index, in which I store the card index. I want to print out the index of the card I click on, just to make sure its all working. My camera is located at 0,0,-10

    I also have a script attached to my scene, and here is the code I have in the Update method so far:

    Code (csharp):
    1. if (Input.GetMouseButtonDown(0))       
    2.         {          
    3.             Vector2 v = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.             RaycastHit2D hit = Physics2D.Raycast(v, Vector2.zero);
    6.             if(hit)        
    7.             {              
    8.                 Debug.Log("card clicked: "+hit.collider.GetComponent<CardPrefabScript>().index);               
    9.             }          
    10.         }
    I admit, I dont really understand the concept of the vectors, the ray and the raycast, it was just something I saw when searching. Does anyone have a minute to explain the process to me? Is it appropriate to add the hit testing to the Update loop of the Card prefab or is it ok to have it in the Scene script?

    Thanks!
     
  2. arncota

    arncota

    Joined:
    Nov 12, 2013
    Posts:
    14
    More trial and error and I got it to work! I had to move the mouse handling to the CardPrefabScript (well at least that worked, maybe there is a way to do it on the Scene script too):

    Code (csharp):
    1.         if (Input.GetMouseButtonDown (0)) {        
    2.            
    3.             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    4.            
    5.             RaycastHit2D hit = Physics2D.GetRayIntersection (ray, Mathf.Infinity);
    6.                        
    7.             if (hit.collider != null  hit.collider.transform == this.transform) {
    8.                 // raycast hit this gameobject
    9.                 Debug.Log("CardPrefabScript:Hit:"+this.cardFaceSprite);    
    10.  
    11.                 if (this.flipped == true) {
    12.                     this.GetComponent<SpriteRenderer>().sprite  = this.cardBackSprite;
    13.                     this.flipped = false;
    14.                 }
    15.                 else {
    16.                     this.GetComponent<SpriteRenderer>().sprite  = this.cardFaceSprite;
    17.                     this.flipped = true;
    18.                 }
    19.             }
    20.         }
     
    Noor-Ali likes this.