How do I make objects stick to moving platforms?

Im trying to make a ball game where a ball just rolls around and jumps and has to avoid falling off the platform

i will be adding elevators and random moving platforms, and my ball just keeps falling off of this animated moving platform that is moving on the Z-axis

UNITY - How do I make object stick to moving platform?

It's just amazing that until today, no one else has ever had problems with colliders on moving platforms before and thought to ask Unity Answers where [Google][1] could find it. [1]: http://lmgtfy.com/?q=unity+collision+"moving+platforms"

1 Answer

1

Use colliders to detect if the Ball is on the platform, when it is you parent the ball to the platform. This way the ball will follow the platform exactly. When you roll off you detect that the Ball is no longer colliding with the platform and unparent it again.

can you tell me exactly how to do that?

Try something like this in your Ball code: private void OnCollisionEnter(Collision c) { if(c.transform.name == "Platform") { transform.parent = c.transform; } } Now you can do the reverse for OnCollisionExit to unparent the Ball again. This code also assumes that your platforms name is "Platform". Also I would recommend reading up on the Unity Manual on [Physics][1] and [Colliders][2]: [1]: http://docs.unity3d.com/Documentation/Manual/Physics.html [2]: http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

i just got a bunch of errors from doing that[19119-untitled1.png|19119]

@GameVortex posted C# and it looks like you are using UnityScript.. function OnCollisionEnter(c : Collision) { if(c.transform.name == "Platform") { transform.parent = c.transform; } }

now my ball is changing its shape, all weird, when it touches the platform [Picture][1] [1]: http://i.imgur.com/7JVduvt.png