Esla Baeny
Esla Baeny

Reputation: 83

How to stop player from automatically jumping on a moving platform?

I made a moving platform that goes vertically top to bottom and then bottom to top and so on. Platform moves fine but placing my player on it makes my player unstable.

When platform moves from top to bottom, my player kinda bounces on it. When platform moves from bottom to top, it remain stable on the way but when it reaches top point, my player makes a jump by itself.

Its get even worse when i increase the speed of my platform. I don't know if its due to unity 2d physics effect or what. I have tried to use physics material 2D on my player object and platform by setting bounce to 0 and friction to 50 but nothing seems to work. Any one have idea how to disable physics effect of moving platform? Following is my code for moving platform:

public class BrickMoveVErtical : MonoBehaviour {
     public Vector3 positionOne;
     public Vector3 positiontwo;
     public Vector3 nextposition;
    /*Empty object is already made on unity editor and its parent of platform(Plank) and other  
       empty object "pointB". Point "B" is already mapped on editor and platform is set to go from 
         its original pos to point B */
     public Transform plankTranform;         
     public Transform positionBTransform;
     public float speed;
     // Use this for initialization
     void Start () {
         positionOne = plankTranform.localPosition;
         positiontwo = positionBTransform.localPosition;
         nextposition = positiontwo;
     }

     // Update is called once per frame
     void Update () {
         move();
             }
     private void move() {
    plankTranform.localPosition = Vector3.MoveTowards(plankTranform.localPosition,nextposition,Time.deltaTime*speed);
         if(Vector3.Distance(plankTranform.localPosition,nextposition)<0.1)
         { changeMovementPlank(); }
     }
     void changeMovementPlank() {
         nextposition = nextposition != positionOne ? positionOne : positiontwo;
     }                        

 }

Upvotes: 1

Views: 1678

Answers (2)

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

You can use this code to set/unset the parent of a GameObject:

void OnTriggerEnter2D(Collider2D other)
{
    if(other.gameObject.tag == "PlatformGroup")
        player.SetParent(other.gameObject.transform)

}

void OnTriggerExit2D(Collider2D other)
{
    if(other.gameObject.tag == "PlatformGroup")
         player.SetParent(null);
}

What you should do is:

  1. Create an empty GameObject, and you make your platform child of this empty GameObject. Tag it as PlatformGroup.

  2. You add the script to move the platform to this EmptyGame Object, instead of using it in the platform itself.

  3. Then add a collider to empty gameobject(PlatformGroup) and another to the Player. Set the one of the player as trigger.

  4. Add the code above to your player controller.

Now, when the Player jumps over the Platform if will become a child of the same GameObject of the platform and they will move together without any deformations in the GameObject. Also when the player leaves the platform, walking or jumping out of it, it will stop being child.

Upvotes: 0

Bshara Zahran
Bshara Zahran

Reputation: 11

I had similar problems with fast platforms in my game, the character would be left out in the air or slide off, depending on the direction of the platform. I found a workaround for the problem by simply changing the character transform.parent to the platform it was jumping on, whenever the player exists the platform collider, simply return the players original transform.parent.

Note that this might cause other bugs or problems that will need to optimize, depending on your game that is.

Upvotes: 1

Related Questions