Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving 2D platforms

Discussion in '2D' started by farrico, Oct 6, 2019.

  1. farrico

    farrico

    Joined:
    Feb 8, 2017
    Posts:
    20
    Hey guys, im trying to get the player 'object' to 'stay' into a moving platform.
    For now, whenever i jump on it the player doesnt 'stuck' into.

    Video reference attached.

    Can anybody help?

     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @farrico

    This is not a Unity specific problem, so google for platformer game articles + moving platform articles, there are several. And there are several Unity specific tutorial videos.

    And you shared very little info about your setup - actually, you didn't share anything... so can't help with the details.

    But in general - what you'll have to do, is to first detect are you on a moving platform, this is just a matter having some trigger or detecting grounded state over some specific object.

    Then the movement part.

    When your platform is moving it has certain velocity vector, which your player doesn't have. You jump up or stand still, your character has no idea about platform so it will jump straight up or slide off from a platform. A solution is usually (depending on choices you made - of which I have no idea because you didn't tell) to add the current velocity of the platform to your character's velocity. So if your platform moves in this frame 1m right, you have to add that to your player's velocity.
     
    Last edited: Oct 6, 2019
    vakabaka likes this.
  3. Ledesma099

    Ledesma099

    Joined:
    May 15, 2018
    Posts:
    26
    For the player to stay on the platform with movement. You only need to make the player a son of the platform, when the player detected through OnCollisionEnter2D, Raycast, Overlap, etc that he touched the platform.

    For make an object a child of another object is:

     object.transform.SetParent(obect2.transform); 


    For undo this is:

     object.transform.SetParent(null); 
     
    vakabaka likes this.
  4. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this can be depended on the input. In some case you can just add more friction.
    https://docs.unity3d.com/Manual/class-PhysicsMaterial2D.html

    If the more friction will not help then you are perhaps move your player over the change in the rigidbodies velocity directly. Something like: rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * speed, rb.velocity.y). Then the zero in the input stops the moving. You can try to disable zero-input on the platform and make player the child from the platform for this time.

    ok, not the exactly script, just an example (maybe it is wrong somewhere):
    Code (CSharp):
    1. void OnCollisionEnter2D (Collision col) {
    2. if (col.CompareTag ("Player") {
    3. col.gameObject.transform.parent = gameObject.transform;
    4. col.gameObject.GetComponent<NameOfTheMovingScript>().isOnThePlatform = true;
    5. }
    6.  
    7. void OnCollisionExit2D (Collision col) {
    8. if (col.CompareTag ("Player") {
    9. col.gameObject.transform.parent = null;
    10. col.gameObject.GetComponent<NameOfTheMovingScript>().isOnThePlatform = false;
    11. }
    then in the players script:
    Code (CSharp):
    1. public bool isOnThePlatform;
    2.  
    3. void FixedUpdate () {
    4. if (!isOnThePlatform) {
    5. //move normal
    6. rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * speed, rb.velocity.y);
    7. } else
    8. if (Input.GetAxis ("Horizontal") !=0 && isOnThePlatform) {
    9. //move on the platform by some input
    10. rb.velocity = new Vector2 (Input.GetAxis ("Horizontal") * speed, rb.velocity.y)
    11. }
    12.  
     
    Last edited: Oct 6, 2019
    Ledesma099 likes this.