Search Unity

Question Moving platform issues

Discussion in 'Scripting' started by johnc81, Sep 22, 2021.

  1. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hello,

    I am implementing moving platforms in my game and have some that go diagonally between 2 points. I have got this working, but there is a bit of a glitch. I am using a Platform Effector 2D, but have noticed 2 things:

    1. If the player does not jump right through the collider from under, the platform appears to drag / push the player a short distance before they fall back down.

    2. Even if the player should land on the platform (they jumped high enough), it seems like the player slows down before landing, causing the platform to continue moving under them and they end up falling to the ground.

    Has anyone else experienced these types of issues and if so, how did you resolve them?

    I have been struggling with this for days and have tried so many different approaches that my code is a complete mess now.

    I have tried setting bools using on trigger, oncollision, setting up raycasts etc., but nothing seems to fix it.

    Many Thanks,

    J
     
    Last edited: Sep 22, 2021
  2. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hello,

    Just wanted to post an update to this. My player character is using a lot of raycasts to detect the world around her. These seemed to be negatively interacting with the moving platform. I could just change the platform to be on the "Ignore Raycast" layer, but that creates another problem because she uses her ground check raycast to detect she has landed.

    The solution I have come up with (can't comment if this is the best solution, but works for me) is to add an empty gameobject to the platform, with the transform.position.y at the surface of the platform. I also added an empty gameobject on my player character with the transform.position.y at her feet.

    I set the layer on the platform to be "Ignore Raycast". Now when the player collides with the PlatformEffector2D and the transform at her feet is above the transform on the platform, I change the layer to "Ground" (this.gameObject.layer = 8;). When she exits the platform, I change it back to "Ignore Raycast" (this.gameObject.layer = 2;).

    This resolves all the issues with the player appearing to be dragged / pushed along by the platform when jumping and also the slow falling when above it.

    Not sure if this is of any use to anyone, but thought I would post for completeness.

    Many Thanks,

    J