- Home /
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
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 could find it.
Answer by GameVortex · Dec 09, 2013 at 01:33 PM
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.
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 $$anonymous$$anual on Physics and Colliders:
@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