Home > Software engineering >  How do i make my player move with a moving platform in unity 2d
How do i make my player move with a moving platform in unity 2d

Time:12-18

I'm making a 2D platformer and decided to add sticky platforms. I've made the platforms move, but the player doesn't move with them.

However, after parenting the player to the platform, the player still falls through. I have added two BoxCollider2Ds and set one of them as a Trigger. None of the colliders have a RigidBody2D

public class StickyPlatform : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.gameObject.transform.SetParent(transform);
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == "Player")
            collision.gameObject.transform.SetParent(null);
    }
}

CodePudding user response:

trigger does not stop object move through the box, you can try OnCollisionEnter. Usually I would have a ground check and you can say if(isGrounded&&onPlatform) //move with platform

CodePudding user response:

So it sounds you have two problems:

  1. Player is falling through platform
  2. Player isn't sticking to the platform

Falling through the floor is a super common bug, with many causes. Here is a checklist I found:

  1. does Object have a Collider? If not, select Object
  2. go to the top bar
  3. components
  4. physics
  5. choose appropriate collider (if Terrain, check the last tab, the little cog-wheel)

Note: mesh-collider may cause problems Particularly, if both FallingObject and GroundObject have mesh-collider Particularly, if the mesh is animated To avoid mesh-collider, you can build an aproximate shape of your mesh from several primitive colliders (in parent-, child- or sibling-GameObjects) If you need a Mesh-collider no matter what, you can try to place additional primitive colliders where they won't be in the way to 'enforce' the collisions

Is the Object a Trigger? If so, select Object

  1. find its Collider-Component (if Terrain, check the last tab, the little cog-wheel)
  2. remove the check of 'IsTrigger'

Is the Collider placed well? Fiddle with center, size and skin-width (start with 0.1) until the green outline aproximately fits the character (If you get really strange values, it might be due to scale (e.g. your mesh was way too big so you downsized to .01))

You may try to zero out all positioning (both unity and your modeling-program)

The link goes into even more cases.

Once the player can stay on, the physics engine should handle the momentum transfer to move the player with the platform using friction. Which again, requires RigidBody2D. I'm not sure why you're not using RigidBodys, it kind of feels like you're avoiding the solution.

Doing it this way should avoid the need to parent the player, and have a trigger volume as well, unless you want the player physically stuck and can't move on the platform.

  • Related