Search Unity

Descending Slopes with RigidBody2d

Discussion in '2D' started by elgatodorado, Sep 15, 2019.

  1. elgatodorado

    elgatodorado

    Joined:
    Apr 7, 2019
    Posts:
    22
    Hello, I am making a platformer 2d with slopes and I am trying to make that my character could descend slopes.

    I am following this tutorial:



    But I have a problem, I am making this with RigidBody2d.

    I tried to adapt this method with a RibigBody2d but I cannot make it work.

    This is how I move my player:

    Code (CSharp):
    1. h = Input.GetAxisRaw("Horizontal");
    2.  
    3. transformada.position = transformada.position + new Vector3(velocidad * h * Time.deltaTime, 0, 0);
    Here it is the code I tried to adapt:

    Code (CSharp):
    1. Debug.DrawRay(transform.position, new Vector2(0, -1.5f),Color.red);
    2.         hit = Physics2D.Raycast(transform.position, new Vector2(0, -1.5f), 1.5f,layerRampa);
    3.  
    4.         if (hit)
    5.         {
    6.             float anguloRampa = Vector2.Angle(hit.normal,Vector2.up);
    7.             if (anguloRampa>10)
    8.             {
    9.                 if (Mathf.Sign(hit.normal.x) == transform.localScale.x)
    10.                 {
    11.                     if (hit.distance-distancia <= Mathf.Tan(anguloRampa*Mathf.Deg2Rad) * Mathf.Abs(myBody.velocity.x))
    12.                     {
    13.                         float movimiento = Mathf.Abs(myBody.velocity.x);
    14.                         float movimientoDescendenteY = Mathf.Sin(anguloRampa * Mathf.Deg2Rad) * movimiento;
    15.                         myBody.velocity = new Vector2(Mathf.Cos(anguloRampa * Mathf.Deg2Rad) * movimiento * Mathf.Sign(myBody.velocity.x), myBody.velocity.y);
    16.                         myBody.velocity = new Vector2(myBody.velocity.x, movimientoDescendenteY);
    17.                     }
    18.                 }
    19.             }
    20.         }
    Thank you for your time