Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

moving platform, but the player don't move with

Discussion in '2D' started by CHRISDL, Aug 13, 2019 at 4:08 PM.

  1. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    well i have a moving platform that i can move with "a" and "f" (i'm french, i'm using azerty keyboard, so the player move with ZQSD) But my player don't move with my platform, i have a physics material 2D with 0 friction and 0 bounciness on my player rigidbody 2D

    here's my platform script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public GameObject Player;
    9.  
    10.     private float platInput;
    11.  
    12.     private Rigidbody2D rb2;
    13.  
    14.     void Start()
    15.     {
    16.         rb2 = GetComponent<Rigidbody2D>();
    17.     }
    18.  
    19.     void FixedUpdate()
    20.     {
    21.         platInput = Input.GetAxis("Platform");
    22.         rb2.velocity = new Vector2(platInput * speed, rb2.velocity.y);
    23.     }
    24.  
    25.     private void OnCollisionEnter2D(Collision2D collision)
    26.     {
    27.         if (collision.gameObject.tag == "player")
    28.         {
    29.             Debug.Log("dessus");
    30.             collision.collider.transform.SetParent(transform);
    31.         }
    32.     }
    33.  
    34.     private void OnCollisionExit2D(Collision2D collision)
    35.     {
    36.         if (collision.gameObject.tag == "player")
    37.         {
    38.             collision.collider.transform.SetParent(null);
    39.         }
    40.     }
    41.  
    42. }
    Capture d’écran 2019-08-13 à 16.24.36.png

    here's my platform
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
  3. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    so.. that didn't work but at least, i have a great bug ! Capture d’écran 2019-08-13 à 18.02.19.png
     
  4. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    Capture d’écran 2019-08-13 à 18.04.58.png
    we have that too now, yes the big white thing is.. my player
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Here is what i do so my player can move on the moving platforms. The script is attached to the player, not the platform. I messed that order up when i first posted this, sorry.

    Code (CSharp):
    1.  void OnCollisionEnter2D(Collision2D other)
    2.     {
    3.         if (other.transform.tag == "MovingGround")
    4.         {
    5.             transform.parent = other.transform;
    6.             Debug.Log("We are on a moving platform");
    7.         }
    8.     }
    9.  
    10.     private void OnCollisionExit2D(Collision2D other)
    11.     {
    12.         if (other.transform.tag == "MovingGround")
    13.         {
    14.             transform.parent = null;
    15.         }
    16.     }
     
    Last edited: Aug 13, 2019 at 6:17 PM
  6. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    Well, it work.. not as i expected Capture d’écran 2019-08-13 à 19.12.43.png
     
  7. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    I edited my last response. Use that script on the player, not the platform. I got it backwards.
     
  8. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    that's what i did in first time, and i'm still getting that bug Capture d’écran 2019-08-13 à 19.27.35.png
     
  9. CHRISDL

    CHRISDL

    Joined:
    Nov 18, 2018
    Posts:
    7
    And i did what you call a "mistake" By putting the script in my platform, and changing "moving ground" with "Player" and it did what's shown on my last screenshot (before this one with the Size bug)
     
  10. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    102
    Odd. What is that platform or other white bar on top of the player? The screenshot in post #6.