- Home /
How to select different objects with raycast (puzzle game)
Hi everyone, I was making a memory puzzle game with 9 cards, the goal is match 3 cards with other cards with the same picture until the 9 cards find his matches(something like this match game). I want to select one of the cards when I click over the object, the problem is when I click any card, only one card is selected and flip and is always the same card,even if I hit different cards. All cards are the same, just with different picture and all have the same tag. How can I fix it? Sorry for my english.
using UnityEngine;
using System.Collections;
public class GameController_script : MonoBehaviour {
public bool win;
public int cardValue = 0;
public string cardName;
public float rotateDegreeSecond = 180.0f;
public bool isFaceUp = false;
public float flip_degree_limit = 180.0f;
public Camera myCam;
private controller_script control;
void Start ()
{
//control = GameObject.FindWithTag("Card").GetComponent<controller_script>();
}
void Update ()
{
if (Input.GetMouseButton(0))
{
clickObject();
}
}
void clickObject()
{
control = GameObject.FindWithTag("Card").GetComponent<controller_script>();
RaycastHit hit;
Ray ray = myCam.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Card")
{
control.activeFlip(); //this make the cards flip, no problem with this
}
}
}
}
Answer by robertbu · Feb 24, 2014 at 04:39 AM
You can use the 'hit' to get the component for that specific hit:
void clickObject()
{
RaycastHit hit;
Ray ray = myCam.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Card")
{
control = hit.collider.GetComponent<controller_script>();
control.activeFlip(); //this make the cards flip, no problem with this
}
}
}
Your answer
Follow this Question
Related Questions
How to touch select 3D objects 2 Answers
inside rubicks cube slice problem 0 Answers
Instantiate Different GameObject prefabs? 1 Answer
How to fill selected area with prefabs? 1 Answer
Raycasting a specific square area 1 Answer