Search Unity

Slippery Dip speed boost

Discussion in '2D' started by OliverTheLove, Dec 8, 2016.

  1. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Hey!

    I am trying to create a slippery dip effect that will boost my player and fling it away, but I don't really know how to create the effect. I posted an image at the bottom of the post, trying to make it clearer what I'm trying to accomplish!
    I tried to make the player slide but it didn't really work that well, since the character just stopped herself and the speed was too slow.

    When it comes to creating addForce, how could I make it at several points on one object?

    This is what I tried creating for sliding down:

    Code (CSharp):
    1.         float move = Input.GetAxis("Horizontal");
    2.         if (onSlipperyDip)
    3.         {
    4.             float targetVelocityX = move * maxSpeed;
    5.             float temp = Mathf.SmoothDamp(rigi.velocity.x, targetVelocityX, ref velocityXSmoothing, 0.8f);
    6.             rigi.velocity = new Vector2(temp, rigi.velocity.y);
    7.         }
    8.         else
    9.             rigi.velocity = new Vector2(move * maxSpeed, rigi.velocity.y);


    This is how I created a trampoline earlier, and thought maybe I can do this effect, but it effects wherever I thouch the object, and only goes into one direction.

    Code (CSharp):
    1.     void OnCollisionEnter2D(Collision2D col)
    2.     {
    3.         if (col.gameObject.name == "Player")
    4.         {
    5.             controller = col.gameObject.GetComponent<PlayerController>();
    6.             controller.onGround = false;
    7.  
    8.             Vector2 jumpForce = new Vector2(0, impulse);
    9.             col.gameObject.GetComponent<Rigidbody2D>().AddForce(jumpForce, ForceMode2D.Impulse);
    10.         }
    11.  
    12.     }
    slipperyDip.png
     
    Last edited: Dec 8, 2016
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    First of all, I assume that you do have a physics material with a friction very close to zero on the slide...

    Secondly, I think part of the problem is the direction in which you set your velocity. It would be better to apply the velocity along the tangent of the surface point upon which your character currently is.
     
    OliverTheLove likes this.
  3. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Thanks for the tip!
    I do have fricton set to 0 right now.

    Is there an easy way to get the tanget of the surface in a PolygonCollider2D? As far as I've understood, I need to find the normal first to calculate it, which is another task to find.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The normal is given to you through the Collision2D object during the OnCollisionEnter2D callback, in the ContactPoint2D in the "contacts" array.

    You could also use raycasts and get the normal from the returned RaycastHit2D object, or do multiple raycasts and average the normals for a smoother ride across sharp changes in angle.

    The Cross Product of two vectors will give you a vector perpendicular to both.

    So getting the cross product of the surface normal, and Vector3.forward, will give you a vector perpendicular to both, and parallel to the ground.

    See this example:
    http://answers.unity3d.com/answers/133724/view.html

    You don't need to do the second check against Vector3.up, because your game is 2D and the normal should never point the same direction as Vector3.forward.

    Then you can either use that vector for your velocity direction, or Project your current velocity onto that vector.
     
    Last edited: Dec 14, 2016
    OliverTheLove likes this.
  5. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Thanks for the reply!
    I think I've somewhat managed to solve it with the tangens function! Just gotta polish it out :)

    I used it with raycasting for now
     
  6. OliverTheLove

    OliverTheLove

    Joined:
    Jun 26, 2016
    Posts:
    24
    Here is the result! :)

    I calculated the tangens on collision enter, and added an impulse onto my character in the direction of the tangens * -1!

    gif.gif
     
    LiterallyJeff and PGJ like this.
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If you used *-1 because the tangent was making the character go the wrong way, that's because the Cross Product gives you left or right side vectors based on the order and orientation of the vectors you give it. Try swapping the parameter order and you won't have to multiply by -1, or keep the order but use Vector3.back instead of Vector3.forward.