Search Unity

Platformer 2D w/ tilemap: Move player with moving-platform.

Discussion in '2D' started by Napoleonite, May 14, 2018.

  1. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    30
    I know how to make the player move with a moving-platform by making it a child of the platform:



    But my problem is that the player has a capsule collider and a zero-friction-physics2d-material applied (to prevent getting stuck in walls from the tilemap like this guy: https://forum.unity.com/attachments/screen_problem-png.143501/ ).
    However that zero-friction-physics2d-material also causes the player not to move along with the moving-platform (took me a week to figure out that this was the cause). Applying a material with friction to the platform obviously does not fix it because zero multiplied by something is still zero.

    Yes applying a friction material to the player obviously solves the moving-platform issue but then I get stuck in every wall from the tilemap again.

    How do I best solve this?
     
  2. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Hi I'm not sure how you would implement this but I'm sure you can apply a different material upon OnCollisionEnter with the platform and then change it back with OnCollisionExit. Just a thought ;)
     
    Napoleonite likes this.
  3. Napoleonite

    Napoleonite

    Joined:
    Mar 15, 2015
    Posts:
    30
    Wow thanks that super simple solution does indeed work (why did I not think of that..). I however cannot drag a Physics2DMaterial into the inspector onto a public variable for some reason but I can make them strings and load it in the Start() function and since enemies require it as well I made a separate Script:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlatformEntity : MonoBehaviour
    4. {
    5.     public string DefaultMaterial = "PM_Default";
    6.     public string PlatformMaterial = "PM_Platform";
    7.  
    8.     PhysicsMaterial2D MaterialDefault;
    9.     PhysicsMaterial2D MaterialPlatform;
    10.     Collider2D Collider;
    11.  
    12.     void Start()
    13.     {
    14.         Collider = GetComponent<Collider2D>();
    15.         MaterialDefault = Resources.Load("Materials/" + DefaultMaterial, typeof(PhysicsMaterial2D)) as PhysicsMaterial2D;
    16.         MaterialPlatform = Resources.Load("Materials/" + PlatformMaterial, typeof(PhysicsMaterial2D)) as PhysicsMaterial2D;
    17.     }
    18.  
    19.     public void SetMaterialDefault()
    20.     {
    21.         Collider.sharedMaterial = MaterialDefault;
    22.     }
    23.  
    24.     public void SetMaterialPlatform()
    25.     {
    26.         Collider.sharedMaterial = MaterialPlatform;
    27.     }
    28. }
    And ohyeah I also had to change the platform material itself from 0.4 --> 1.0 friction.
     
  4. N_Murray

    N_Murray

    Joined:
    Apr 1, 2015
    Posts:
    98
    Looks good:) and its always the simple things that catch us out , sometimes you just need to take a break or get someone else to have a look ;)