2D Matching Game Question
Hi all I am currently working on a game where you must click on colour pairs to match them and then they disappear. I am using 2D sprites to do this but I am struggling in terms of the logic to erase the pair when both is clicked via mouse.
Here is what I want to happen: When the game starts it picks 3 colours from an array of 6 then randomly places them (2 of each colour) on the screen. You then have to click the colour for example green (it will highlight) then click on the other green, and they will both disappear. If you were to, say, click on the green first then yellow, the game will just end.
This is the code that I have implemented at the moment:
if (Input.GetMouseButtonDown(0))
{
CastRay();
}
}
function CastRay() {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
// Number is the amount of objects on the screen at one time.(6)
number --;
//Test to see if a mouse click interacts with the 2D Sprite.(Then destroys it)
Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position + gameObject.tag);
Destroy(hit.collider.gameObject);
}
// This when the number hits 0 the level restarts (To check random elements)
if (number == 0)
{
Application.LoadLevel (0);
}
}
Thanks in advance.
Thanks for getting back to me! This is the code that I have implemented at the moment-.
Would it be best to give every colour a tag? When the game starts its picks 3 colours from an array of 6 then randomly places them (2 of each colour) on the screen. You then have to click the colour for example green (it will highlight) then click on the other green, and they will both disappear. if you was to say click on the green first then yellow, the game will just end.
Here is my code -
if (Input.Get$$anonymous$$ouseButtonDown(0))
{
CastRay();
}
}
function CastRay() {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(hit.collider != null)
{
// Number is the amount of objects on the screen at one time.(6)
number --;
//Test to see if a mouse click interacts with the 2D Sprite.(Then destroys it)
Debug.Log ("Target Position: " + hit.collider.gameObject.transform.position + gameObject.tag);
Destroy(hit.collider.gameObject);
}
// This when the number hits 0 the level restarts (To check random elements)
if (number == 0)
{
Application.LoadLevel (0);
}
}
Answer by Statement · Mar 08, 2016 at 08:53 PM
On click:
If not have clicked anything yet, store the clicked object.
Else if color is of same kind (and adjacent), make both disappear/erase.
Else, store the clicked object.
Your answer
Follow this Question
Related Questions
Creating Perfect Circle That Can Get Narrower - 2D 0 Answers
Player Positioning on a Rotating Circle 0 Answers
following round transparent around player (2D) 0 Answers
How to get Vector3.Distance from the perimeter of a circle? 1 Answer
How to calculate vector perpendicular to a direction vector? 2 Answers