Search Unity

Help with moving Platform

Discussion in '2D' started by fosmark13, Jul 14, 2016.

  1. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    hi i'm doing a platform where my character can hang from it and the platform moves in different directions, of course the player should move with the platform as well, currently i have some "hanging objects" and my player can hang from them but they don't move, my player compares the hanging through a LayerMask and a tag for different purposes but this is how i do it:

    this is how i check the hanging objects:

    Code (CSharp):
    1. touchingHangingArea = Physics2D.OverlapCircle (hangingAreaCheck.position, hangingAreaCheckRadius, whatIsHangingArea);
    and when the "hanging object is detected this is what the player does"


    Code (CSharp):
    1. if (touchingHangingArea && !grounded) {
    2.  
    3.  
    4. rBody2D.gravityScale = 0;
    5.  
    6.  
    7. GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, 0);
    8.  
    9. }
    Basically just turn to zero the GravitaScale of the player's rigibody, this works as i expected, but now since the platform is moving i'm trying to get the player a child of the moving platform but the player never change its gravityScale, i put this script in the child hangingCheck (which is a child of the player and the game object that i use for position of the OverlapCircle )

    Code (CSharp):
    1.  
    2.  
    3. void Start () {
    4.  
    5.         playerGameObject = GameObject.FindGameObjectWithTag ("Player").GetComponent<fos_controller_script>();
    6.  
    7.         playerRigibody = GameObject.FindGameObjectWithTag ("Player").GetComponent<Rigidbody2D> ();
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.  
    16.     void OnTriggerEnter2D(Collider2D other){
    17.    
    18.  
    19.         if (other.transform.tag == "hangingOnMovingPlatform") {
    20.  
    21.             playerGameObject.transform.parent = other.transform;
    22.  
    23.             playerRigibody.gravityScale = 0;
    24.  
    25.             playerRigibody.velocity = Vector3.zero;
    26.  
    27.  
    28.        
    29.  
    30.         }
    31.    
    32.    
    33.     }
    34.  
    35.     void OnTriggerExit2D(Collider2D other){
    36.  
    37.  
    38.         if (other.transform.tag == "hangingOnMovingPlatform") {
    39.  
    40.             playerGameObject.transform.parent = null;
    41.  
    42.             playerGameObject.isOnHangingMovingPlatform = false;
    43.  
    44.  
    45.  
    46.  
    47.         }
    48.  
    49.  
    50.     }
    51.  
    52. }
    53.  
    The player is getting child of the platform but it never stops its velocity, for example if the platform is above the player and the the player jumps and touches the hangingArea it should stop and be hanging there like in my other hangingObjects, but it just reduces the player speed and continues with its jump!!!

    i don't know why is doing that, i already tried putting a collider in the player instead of a child but works the same... the player should stop there, thats it!!!

    any help would be appreciated
    thanks!!!!
     
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Have you tried putting another collider on it that isn't a trigger? Or just changing the collider you have from a trigger to a normal collider (& change ontrigger to oncollision)?
     
  3. fosmark13

    fosmark13

    Joined:
    Feb 16, 2015
    Posts:
    91
    Yes, i tried both, even with the OnCollisionEnter2D instead of OnTriggerEnter2D and the same result, it most be something of the rigibody getting child of the hanging platform that is not making it change its gravity scale, but i don't know exactly how this parent child stuff work