Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Stacking Objects on Movable Platform

Discussion in 'Scripting' started by zoul, May 5, 2007.

  1. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    Forgive me if this has been answered before, I failed to find anything that seemed to answer my question in the past 2 days.

    I'm using the Unity Trial and testing the engine out, and so far, I must say, "WOW!".

    I am attempting to make a movable platform, in which I can stack objects onto it and the stacked objects move with the platform. However, if the platform moves too fast, I want the objects to start to slide off ... if the platform moves slow enough, the objects stay in place on the platform --

    If you've played Mario Ware Smooth Moves on the Nintendo Wii, I'm trying to replicate the game play in the in the dropping block tetris style game ... blocks fall from above, and land on the platform ... you have to catch all the blocks on the platform to win the match ...

    Anyhow, I've looked into using Physics Materials to create a high amount of friction and this did not work ... I tried using the Rubber Physics Material from the standard assets, and I even created an OnCollisionEnter event handler in C# that parented the collider with the platform ...

    Oddly, when I parented the collider with the platform, with collision.transform.parent = transform in the script attached to the platform ... the falling block appeared to go "whacky" ... it's shape became all distorted, etc ...

    Short and simple, I'd like to know how to create a friction which would allow the falling block to stay on the platform, but if the platform moves at a rapid speed the block begins to slide off the platform (imagine standing on a board that is moving, if it moves too fast you'll fall off)


    thanks in advance ...
     
  2. MatthewW

    MatthewW

    Joined:
    Nov 30, 2006
    Posts:
    1,356
  3. zoul

    zoul

    Joined:
    May 5, 2007
    Posts:
    50
    Matthew, thanks for the quick reply ... I read about the Fixed Joint stuff last night ... and I thought it was the direction to go, until I clicked 'next' a few times in the docs and saw Physics Materials ... ;)


    However, my problem with the fixed joint is ... I can set a break force, so the cube on the platform will break it's joint if the platform moves with enough force ... however, my delima here ... because I'm not all that great with the concept of physics or anything (or maybe I'm over thinking this?) ... is ... how do I know when to create another fixed joint so that the cube re-attaches to the platform ...

    When I just drop cubes onto the platform, and move the platform ... the cubes stay in place, and have a really mild reaction to the platforms movement (evidenced by selecting the cube in the scene view and watching it's axis markers twitch a little) ...

    So ... when the cube is attached to the platform with a fixed joint, and I set a break force ... and the force between the cube and the platform breaks the joint so the cube appears to 'slide' off the platform ... how do I know when to reattach the fixed joint ... I guess I could check the speed of the platforms movement, and if it's within a certain value, just create another joint ... but, now i'm left with ... is creating upwards to a few thousands joints in a single scene through script that can break almost immediately ... going to effect performance?


    Am I over thinking this whole process? Is it much more simpler then I am thinking it is with the fixed joint concept?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's expected behavior if (as I assume) your platform and blocks have different scales. A child object will inheret the scale of its parent, so if you have differing scales between parent and child, it will go all wacky (as you say ;) ), unless you set the scale of the child to compensate. For example, if you use the editor to parent a cube with scale 2,2,2 to an object that has a scale of 1,1,1, you'll see the child object's scale change to .5, .5, .5 in order to preserve its size.

    Yep. :) What you want to do can actually be a bit tricky (as I discovered some time ago), but no need to mess around with scripting (not much anyway) and joints and stuff. The problem is that you need the blocks and the platform to be rigidbodies in order for the physics engine to process them all accurately when they're all moving. So your platform can't just have a collider, nor can it be a kinematic rigidbody.

    If you remember your "equal and opposite reaction" and all that physics jazz, you soon realize your platform is affected by the falling blocks. So make the platform have way more mass than the blocks. And you have to move it around using rigidbody.velocity, not transform.position. Even with a lot more mass, you still might want to put "rigidbody.velocity.y = 0" into a FixedUpdate function to prevent the platform from moving vertically even a little bit when blocks fall on it.

    That should do it, but also don't forget about the difference between static friction and dynamic friction, to get exactly the sort of behavior you want.

    --Eric