- Home /
Rigidbody doesn't move on moving platform
Hey all!,
I've started making a game which a cube lands on a platform. However when the cube lands on the platform and the players tries to move the platform the cube doesn't move, whereas the platform does.... When the platform is no longer under the cube, the cube does fall...
I'm really confused on how to fix this.
Any help on how i can get the cube to move with the moving platform would be greatly appreciated.
Answer by Kleptomaniac · Apr 04, 2012 at 02:42 PM
Possibly try parenting the cube to the platform? Not sure if its the best way to go but, give it a go:
function OnCollisionEnter(collision : Collision) {
if (collision.collider.gameObject.tag == "Platform") {
gameObject.parent = collision.collider.gameObject;
}
}
Attach this to your cube and tag your platform as "Platform". Not sure if this will even work ...
Hope that helps, Klep
Answer by kalvinlyle · Apr 04, 2012 at 09:46 AM
The cube needs a script that assigns it a position offset relative to the platform. I have no idea if this code will work, but it's basically what I'm doing for my game.
public class PlatformBase : Monodevelop {
Vector3 relativeOffset;
Transform child;
Bool riding;
void Awake ()
{
child = Find("Cube");
}
void LateUpdate ()
{
if(child && riding)
child.transform.position = this.transform.position + relativeOffset;
}
void OnTriggerEnter(Collider other)
{
if(other.name == child.name)
riding = true;
relativeOffset = other.transform.position - this.transform.position;
}
void OnTriggerExit(Collider other)
{
if(other.name == child.name)
riding = false;
}
}
The code doesn't work :( and im still a little confused. If someone could give me a overview with javascript i'd greatly appreciate it.
Answer by Datael · Apr 04, 2012 at 02:51 PM
There's a couple of things that I can think of that could cause this:
It might be because you haven't set the physics material on the platform to anything? You can set a default physics material for all objects in the Project's Physics Settings.
Another possibility: are you moving the platform by directly adjusting its transform (such as in code or in an animation)? I noticed that when I was trying this it would not always properly update the mesh's bounds and seemed to give erratic results with regard to the physics.
I'm on my phone right now so I can't check properly what may be causing the problem, nor check that my solutions will actually work for you (apologies), however, adding the objects as children to the platform is definitely not the solution.
Answer by jorjdboss · Nov 05, 2013 at 10:58 AM
If you're moving the platform using an animated rigidbody with 'isKinematic=true', then you need to enable 'Animate Physics' under the animation component. This will let your moving platform interact with other rigidbodies(in this case your cube).
Note: This only works if you're using kinematic rigidbodies and moving them through animation curves. In case you haven't been using a rigidbody on the platform at all, keep this in mind: all moving colliders are dynamic and must have a rigidbody component attached.
Hope that helps.
Your answer