Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How do i keep several objects inside another object

Discussion in 'Scripting' started by danialgifani, May 25, 2012.

  1. danialgifani

    danialgifani

    Joined:
    May 25, 2012
    Posts:
    4
    Hi,

    I am designing chines dice game , I need to keep 5 dice in Cup all the time and while the cup drag or shake the dice position change accordingly. I made dice as child of Cup but when i drag the cup , dice and cup move together without any collision between dice and Cup. if i didn't make dice as child of cup when i drag the Cup , dice throw out the cup.
    I use box collide for dice and mesh collier for my cup.
    kindly please help me how to keep dice always in cup with maintain collision feature between dice and mesh of cup.

    Thanks
     
  2. Sajid_farooq

    Sajid_farooq

    Joined:
    May 23, 2012
    Posts:
    238
    Dainal, it seems that your Cup and Dice are not responding to physics. Make sure that both the cup and the dice are rigidbodies.

    Also, remember that physics will only come into effect when you "play" the game. If you drag the cup or dice in the editor, they will pass through each other, despite having colliders turned on.

    This will help:
    Physics Tutorial
     
  3. snaker

    snaker

    Joined:
    Feb 25, 2012
    Posts:
    11
    make sure that you have choosed "generate colliders" for your models before put them into your scene.
     
  4. danialgifani

    danialgifani

    Joined:
    May 25, 2012
    Posts:
    4
    Hi sajidfarooq , Thanks for your reply , I have used Rigidbody for both dice and cup and I use touch screen in play mode to drag the cup but dice pass through the cup , let me explain more, I create objects on 3D Max and then add them to project. when I add cup to my project I add mesh collides and Regidbody and set my mesh filter and materials based on object that i created in 3D max , in mesh collides checked the Convex.
    1- Can you tell me is it correct that i define dice as child of cup ?
    2- if not do have any suggestion or solution to keep them in cup all the time?
    3- how to configure engine (collide) to make collision between the side of cup and dice?
     
  5. Zethariel1

    Zethariel1

    Joined:
    Mar 21, 2012
    Posts:
    439
    Unparent the dice. They need to move regardless of cup position, as this is the effect you want to achieve.

    To make sure that the dice don't get out of the cup, create an invisible box collider on top of the cup and parent it to the cup. When you decide to throw the dice, de-activate the box so the dice can get out.
     
  6. Sajid_farooq

    Sajid_farooq

    Joined:
    May 23, 2012
    Posts:
    238
    1 and 2 have already been answered above. For 3, remember not to move your dice directly using transform.Translate. You should use physics (forces), otherwise the results are physically inaccurate and can cause collision detection to fail.

    Code (csharp):
    1.  
    2. void FixedUpdate() {
    3.         rigidbody.AddForce(Vector3.up * 10);
    4.     }
    5.  
     
    Last edited: May 25, 2012
  7. danialgifani

    danialgifani

    Joined:
    May 25, 2012
    Posts:
    4
    Hi
    Thanks , I create invisible box as parent of Cup but dice come up of this box and when i run the game.when this box hit with other dice collide is happened, I think box collision just work while collide from outside of box not inside box. I Dont want to throw dice out of cup.


    I also create empty box in 3d Max and add to my Project, and use mesh collide, and rigidbody. the dice pass through this object from side but not from bottom and I couldnt realize that. Could please help me to solve this problem

    Thanks
     
  8. muzel

    muzel

    Joined:
    May 8, 2012
    Posts:
    6
    try use on cup :- mesh collide smooth spere
    - not convex
    - rigidbody

    on dice : - box collider
    - rigidbody with interpolate and continous dynamic. and gravity
    move only cup.