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 Discorides · Aug 11, 2013 at 06:06 AM · movement script

What is the best method to control the movement of a group of objects based on the collision trigger of any individual?

I'm not sure what the best approach to this is, but I have searched around a lot for some solution but nothing I try seems to work.

I want to have a group of four cubes fall down (tetris style), and when one of them hits the ground, or a previously grounded cube, for all members of the group to stop falling and snap to a grid. The reason I don't want to set the arrangement to be a single object is that they are being shot at and destroyed on their way down.

At the moment I have a empty prefab "shape" with the four "cubes" in it. The shape script just sets the initial position of the group and nothing else. The cube script is below. I've tried a lot to set some sort of groupFalling variable in the shape, but I cannot for the life of me work out how to get a cube to access and change it. Nor am I sure that is the best approach.

My current cube script looks like this (just in case it helps):

 using UnityEngine;
 using System.Collections;
 
 public class Cube : MonoBehaviour {
     private Transform myTransform;
     private float cubeSpeed = 5;
     public bool falling;
     
     // Use this for initialization
     void Start () {
         myTransform = transform;
         falling = true;
     }
     
     // Update is called once per frame
     void Update () {
         if (falling) {
             myTransform.Translate(Vector3.down * cubeSpeed * Time.deltaTime);
         }
     }
         
     void OnTriggerEnter(Collider collider) {
         if (collider.gameObject.CompareTag("Bullet") && falling) {
             DestroyObject(this.gameObject);
             DestroyObject(collider.gameObject);
         }
         if (collider.gameObject.CompareTag("Floor")) {
             falling = false;
             transform.gameObject.tag = "Floor";
         }
     }
 }
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
Best Answer

Answer by highpockets · Aug 11, 2013 at 07:36 AM

Give all your cubes a "Cube" tag and when one hits the floor, do something like this:

         public GameObject [] cubes;
         void OnTriggerEnter(Collider collider) {
             if (collider.gameobject.tag == "Floor")
     {
                 cubes = GameObject.FindGameObjectsWithTag("Cubes");
             
             foreach (GameObject cube in cubes)
     {
              cube.tag = "Floor";
             }
           }
         }

And above where you translate when falling is true, you could do something like this:

 if (gameObject.tag != "Floor") {
          myTransform.Translate(Vector3.down * cubeSpeed * Time.deltaTime);
        }

In this case you won't need your falling boolean variable.

Comment
Add comment · Show 5 · 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 Discorides · Aug 11, 2013 at 07:40 AM 0
Share

That sounds like a promising approach, though I think it will mean that if more than one group is falling at a time then all of them will freeze. I could cycle through a set of tags (cube1, cube2, etc) up to a limit of however many groups will be falling at once. Do you think that would be the best approach?

avatar image highpockets · Aug 11, 2013 at 07:46 AM 0
Share

Personally that is what I would do. I'm sure there is another way to do it, but this is the first thing that popped into my $$anonymous$$d. If you read what I put right away, take another glance because I had a couple errors that I had to edit.

avatar image Discorides · Aug 11, 2013 at 08:04 AM 0
Share

That works, thank you!

avatar image highpockets · Aug 11, 2013 at 08:06 AM 0
Share

Another thought. You could also have an empty game object that parents each group during the fall.

avatar image Discorides · Aug 11, 2013 at 09:35 AM 0
Share

I was initially trying to do that, but I couldn't work out how to get them to communicate. I would be interested to see how you would implement a parent object. I'm new to program$$anonymous$$g and still getting my head about that aspect of things.

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

15 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

Related Questions

Player Sliding without any Input 2 Answers

How to maintain speed on the x axis according the last key pressed ? 2 Answers

Character Transform shifting after colliding with objects 0 Answers

I want to move the object more smooth way 1 Answer

Jerky movement of the 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