Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Tuism · Apr 09, 2014 at 03:28 PM · collision detection

Simplest way of detecting if there's an object at position X

Hi guys.

So I'm making a tetris-like game, and right now I have something that Instantiates blocks into a GameObject, and the GameObject moves in a direction, one unit at a time, every x time period.

Now I want to have each of those blocks to check ahead of themselves when they are supposed to move, and if there's a block there with tag "block_stopped", it will tag all the blocks in the object (there are always four) with "block_stopped", and break the GameObject up.

Each block object has a SpriteRenderer and BoxCollider2D.

There are four Block GameObjects inside of an empty GameObject called p1DroppingController, and the script that controls the dropping is called p1DroppingScript.

I used a function but this function can't be called... CheckPosForWall doesn't autocomplete and mono doesn't recognise that it's there at all. Why?!

 void Update () {
     if (dropCount <= 1f) {
         dropCount += Time.deltaTime;
     } else {
         dropCount -= 1f;
         // check collision
         foreach (Transform genericBlock in transform) {
             if (CheckPosForWall(new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z))) {
             }
         }
         } else {
             transform.position = new Vector3 (transform.position.x, transform.position.y+1f, transform.position.z);
         }
     }
 }

 bool CheckPosForWall (Vector3 where) {
     Ray ray = Camera.main.ScreenPointToRay (where);
     Debug.DrawLine(Camera.main.transform.position, Camera.main.ScreenToWorldPoint(where));
     RaycastHit2D hit = Physics2D.GetRayIntersection (ray, Mathf.Infinity);
     if (hit.collider != null) {
         if (hit.collider.transform.tag == "block_stopped") {
             Debug.Log('y');
             return true;
         } else
             Debug.Log('n');
             return false;
     } else
         return false;
 }


Thanks guy!!!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Apr 09, 2014 at 04:16 PM

Standard trick is think of it as a grid. Every item is either in/claiming one grid space, or is moving into another. While moving to a new space, it claims both (or maybe 4, it it moves diagonal,) then gives up the old space when it reaches the new. No rayCasting at all. In games where units can't freely overlap, you can often see this (like the original WarCraft.)

Setup maybe like int[,] Board = new int[20,20];. Where -1 means free, otherwise the # of the block in it. A better one might be to make a small class for each space, with maybe busy/free, the Transform, the big block, maybe which part of the big block... .

Of course, the actual move involves converting board spaces to world coords. But all the "thinking" is done in grid numbers. Like it moves from x=2 to 2.1, 2.2 ... 3. The picture in game might really be moving from world 50 to world 55.

For groups of blocks, like a certain Russian block game, would add a rule that if the space you want to go into is occupied, but is part of the same big block, it's fine. Then make sure you free/release in the correct order (the behind block claims a space, but the ahead block frees it!!)

Comment
Add comment · 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

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

22 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

Related Questions

Script won't detect collision 1 Answer

Coin collection script (not working) 1 Answer

I have no idea how to check if two 2D objects are colliding 2 Answers

OnParticleCollision not being called on Rigidbody 1 Answer

Collision detection problem 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