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 DirusCanis · Dec 26, 2014 at 10:49 PM · transformobject reference

Pass command from one object to the next

Working on a project that is built around a 3D pyramid of cubes. When a cube is destroyed the one above it needs to drop down to take it's place.

I was thinking I could just tell the next cube up to move with transform.Translate(x,y,z,blah), however I need to figure out how to send that command to the correct cube.

Anyone have any pointers on how I can go about doing this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DirusCanis · Dec 28, 2014 at 10:49 AM

Alright, got my implementation to work. I created a script that is attached to each prefab cube that handles a number of things including randomizing the color/texture on start, assigning a variable based on spawn position, and of course handle the fall code.

 public void Fall(){ // Handles moving the cubes down the pyramid by checking to see if the space below is empty
     tarPos = ownPos; // used for all cubes
     tarPos2 = ownPos; // used for corner cubes only
     tarPos3 = ownPos; // used for corner cubes only
     tarPos.y -= 1.0f; // We're always looking down 1 level.
     tarPos2.y -= 1.0f;
     tarPos3.y -= 1.0f;
     if (cType == 8) { // Side Cube, Move Straight Down
             tarPos.z += 1.0f;
         if (checkEmpty (tarPos)) { // Pass pos to checker
             if (true) {
                 transform.Translate (0f, -1.0f, 1.0f, Space.World);
                 }
                        }
      }else if (cType == 1) {// corner cube, need to check 3 positions
             tarPos.x += 1.0f;
             tarPos2.x += 1.0f;
             tarPos2.z += 1.0f;
             tarPos3.z += 1.0f;
             if (checkIfPosEmpty (tarPos)) {
                 if (true) {
                     transform.Translate (1.0f, -1.0f, 0f, Space.World);
                     cType = 5;
                 }
             } else if (checkIfPosEmpty (tarPos2)) {
                 if (true) {
                     transform.Translate (1.0f, -1.0f, 1.0f, Space.World);
                 }
             } else if (checkIfPosEmpty (tarPos3)) {
                 if (true) {
                     transform.Translate (0f, -1.0f, 1.0f, Space.World);
                     cType = 8;
                 }
             }
         }
 
 public bool checkEmpty(Vector3 tarPos) { 
         GameObject[] cTiles = GameObject.FindGameObjectsWithTag("cubeTile");
         foreach(GameObject current in cTiles) { 
             if(current.transform.position == tarPos) return false;
         } 
         return true; 
     }
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 richyrich · Dec 28, 2014 at 12:58 PM 0
Share

There should be a little tick next to your answer (on left). Select it to let others know the question has been answered to your satisfaction

avatar image
0

Answer by Navigatron · Dec 26, 2014 at 10:57 PM

In your situation, with a lot of cubes, I would create two cubes, one directly atop the other. Make both these cubes the children of some empty object, where the top cube is at 0,0,0 and the bottom cube is at 0,-1,0. Remove the renderer from the bottom cube, and set its Collider to isTrigger. Save this as a prefab

You now have a prefab cube that can detect when things under it move. From there, each cube can handle its position independantly, with no need for some master class to have references to every cube.

The above setup and a basic script like this one should work fine.

(C#)

 void OnTriggerExit(Collider other)//When a Cube below us has been destroyed, or moves downwards
 {
     transform.parent.position = other.transform.parent.position;
 }

I haven't tested this code, you might have to add Vector3.up or down depending on your results.

Best of Luck, ~Navi

Comment
Add comment · Show 3 · 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 DirusCanis · Dec 27, 2014 at 08:03 AM 0
Share

I just re-read my OP, and I did not explain things well at all. The object has to look for an empty spot that is down one unit and out from the center one unit as it's a stepped pyramid. The corner blocks have to check for 3 empty spaces, one of which being the next one down on a diagonal.

I'll have to play with this parent object method and see if it works, if the current attempt of $$anonymous$$e doesn't.

$$anonymous$$y current attempt is to add a script to the preFab cubes that assigns them a type based on their original spawn position, then based on the "type" check the appropriate position to see if it's empty, if it is, then it moves down.

avatar image richyrich · Dec 27, 2014 at 10:28 PM 0
Share

Are you attempting to create some kind of 2D candy crush game but as a pyramid ins$$anonymous$$d of a square where the only squares that can be hit are the ones at the bottom?

avatar image DirusCanis · Dec 28, 2014 at 10:47 AM 0
Share

Not exactly, it is a match 3 based game, but it is ment to be a party game and the board is a 3D pyramid that you can rotate.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Finding objects on scene change 1 Answer

transform.Translate moving while upside down? 1 Answer

Using deviceOrientation to calibrate Camera 0 Answers

What is the reasoning of multiplying by Time.deltaTime 1 Answer

Accessing children of instances vs children of original prefab 1 Answer


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