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 /
avatar image
0
Question by kongsol · Aug 15, 2017 at 10:25 AM · 2d gamecollider2dboardgame

2D Tilebased game with blocks. Only allow to place in corners.

Hello everyone! Im trying to make a tilebased game much like the board game Blockus. I know there are many like it but im making it to learn unity better.

I'm trying to make the blocks only get placed so the corners are the only thing the can touch.alt text

Not like this: alt text

I have colliders on tht block itself and a seperate child gameobject with colliders on the corner so that its only allowed to be placed when triggering with the corner collider. But it still gets placed alongside the other block. I know i can just add another collider to prevent this, but its alot of work since there is about 20 different blocks.

Is there a way to script it so it only places in the corners?

Here is some of the placement script:

 public static GameObject selectedTile;
 
     private Vector2 mousePos ;
     private LevelBoard levelBoard;
 
     bool isColliding = false;
     bool tileIsSelected = false;
     bool hasBeenPlaced = false;
 
 
     void Update(){
 
         if(tileIsSelected == true && hasBeenPlaced == false){
             mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             transform.position = new Vector2 (Mathf.Round (mousePos.x), Mathf.Round (mousePos.y));
         }
 
         if(tileIsSelected == true)
         {
             SelectedTile ();
         }
     }
 
     void OnMouseDown(){
         tileIsSelected = true;
     }
 
     void OnTriggerEnter2D(Collider2D triggerEnter){
         if (triggerEnter.gameObject.tag == "PlayerTile") {
             isColliding = true;
             Debug.Log ("Triggering with" + name)
         }
     }
 
     void OnTriggerStay2D(Collider2D triggerStay){
         if(triggerStay.gameObject.tag == "PlayerTile"){
             isColliding = true;
             if (tileIsSelected == true) {
                 ChangeColorRed ();
             }
         }
 
     }
     void OnTriggerExit2D(Collider2D triggerExit){
 
         if (triggerExit.gameObject.tag == "PlayerTile"){
             isColliding = false;
             if (tileIsSelected == true) {
                 ChangeColorBack ();
             }
         }
     }
 
 
     void SelectedTile(){
         
         if (tileIsSelected == true) {
             
             if (Input.GetMouseButtonDown (1)) {
                 transform.Rotate (Vector3.forward, 90f);
             }
 
             if (Input.GetMouseButtonDown (0) && isColliding == false && LevelBoard.tileOnLevelBoard == true 
                 && LevelBorder.tileOnLevelBorder == false && AllowedColliders.allowedPlacement == true) {
 
                 SpriteRenderer[] renderer = GetComponentsInChildren <SpriteRenderer> ();
                 foreach(SpriteRenderer r in renderer){
                     r.color = new Vector4 (r.color.r, r.color.g, r.color.b, 255);
                 }
 
                 tileIsSelected = false;
                 hasBeenPlaced = true;
                 LevelBoard.tileOnLevelBoard = false;
 
             }
         }
     }


Thanks for your time !

problem-2.png (305.7 kB)
problem1.png (304.3 kB)
Comment
Add comment · Show 9
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 Cherno · Aug 15, 2017 at 02:07 PM 0
Share

Use a two-dimensional array to keep track of the tile information. For example, you get use int[fieldWidth,fieldLength] where each element [x,y] is either a 0 or 1, and 0 means empty while 1 means block. Then you can easily deter$$anonymous$$e the neighbors of any given tile [x,y] by going [x+1,y] and so on and check if there's a block at this adjacent tile position.

avatar image kongsol Cherno · Aug 15, 2017 at 06:15 PM 0
Share

Thank you for your answer, not quite sure how to set it up like that. I dont have much experience with arrays in general.

avatar image Cherno kongsol · Aug 15, 2017 at 08:23 PM 0
Share

Well, I suggest getting acquainted with some basic collection types like arrays, lists, and dictionaries, since you will use these three a lot and they upon up many possibilities that would be cumbersome or impossible to do without. I also took quite a long time before I learned to use collections :)

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

75 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Polygon Collider 2d doesnt work properly 3 Answers

Button above Collider is being ignored when clicked. 0 Answers

Use transform.position in FixedUpdate to ensure collision with objects which have Velocity 0 Answers

BoxCollider2D have an offset on collision,Collider2D has an collision offset? 0 Answers

Is there a way of casting Rays at the end of the collider of an object ? 0 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