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 taiomi · Mar 08, 2016 at 06:26 PM · 2donclickcirclematchingcircles

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.

alt text

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.

screen-shot-2016-03-08-at-181736.png (12.3 kB)
Comment
Add comment · Show 1
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 taiomi · Mar 09, 2016 at 06:22 PM 0
Share

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);
  
      }
          }

1 Reply

· Add your reply
  • Sort: 
avatar image
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.

Comment
Add comment · Show 1 · 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 taiomi · Mar 09, 2016 at 03:30 PM 0
Share

I don't really understand what you mean, but thank you

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

64 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

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


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