Search Unity

[Unity 4.3] 2D physics - SpeedUp Platform/Slope [C#]

Discussion in '2D' started by sebi3110, Jan 16, 2014.

  1. sebi3110

    sebi3110

    Joined:
    Oct 11, 2012
    Posts:
    17
    Hey all!

    I'm currently developing a little 2D Platformer game which involves jumping through a 2D World. My goal is to get the player speed up on Slopes downhill(like 45°) and on specific horizontal "Speed Up" Platforms.

    Furthermore I'd like to have the player sliding a lttle bit(in horizontal direction) after getting on the ground after a jump.

    I discovered the 2D Physic Materials where I can adjust the Friction and Bounce value from 0 to 1. But the friction property does not deliver the function I was aiming for

    I tried to write two little scripts that I attach to colliders where I either speed up the player or have just a little gliding effect after a jump.

    For slopes I thought abouth getting the tangent vector the player has according to the surface and speed the rigidbody2d up in the direction of the tangent vector, but I didn't have any idea how to tackle this. Same with the gliding feature..

    Do somebody has a little hint or script example where I can go from and investigate further more?

    I would be really glad to hear from you guys!
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    If you're using rigidbody.2d.AddForce() or velocity to move the player left/right, then there isn't any reason they shouldn't come to a sliding stop if you're friction values are low enough. If you're using transform.position, then it won't work.
     
  3. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Why aren't the Materials working for you? What's happening?
    Why aren't the scripts attached to the colliders working for you? What's happening?

    I ask this because you are following the correct approach, so I'm interested why it's not working for you.
     
  4. sebi3110

    sebi3110

    Joined:
    Oct 11, 2012
    Posts:
    17
    Thanks for your replies :)


    @ Invertex, I set the friction value to Null, but for example on slopes the Player doesnt speed up as he should. He's sliding pretty slow downwards.... I want to have a more "iceish" 2D material

    @Kurius, I don't know how I can get the tangentvector of the ground/slope point the player stands on...
    If I would have the tangentvector (updating every frame) I could easily speed up the rigidbody2D in this tangent vector direction..

    Cheers!
     
  5. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Don't know if it can help you, but...
    Take a look here http://catlikecoding.com/unity/tutorials/runner/ its a runner tutorial but look at the "Platform Variety" part, he teaches how to do some slow down and speed ups plataforms :)
     
  6. sebi3110

    sebi3110

    Joined:
    Oct 11, 2012
    Posts:
    17
    Thank you! But I have read the article already :)
    The point is that the Physic2D material doens't have options like Static or Dynamic Fraction and it also doesn't work like the 3d equivalent.
    Thats why I have to write a script that imitates a iceish like material where the player slips over...
     
  7. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    I'm not so experienced with the static and dynamic friction.
    But i just done some tests here.

    If i make 2 materials, put both of them with 0 friction, then make 2 objects, both of them with BoxCollider2D each one with a different material.
    And one of them with a rigidbody (the player).
    Then i add 7 force to the player on the start() like this:

    Code (csharp):
    1. rigidbody2D.velocity = new Vector2(7,0);
    The player go all the way without slowing down.

    Then i set the friction of the plataform to 1 (the max) and leave all the rest the same.
    The player go all the way without slowing down.

    Then i set the friction of the player to 0.1 and leave all the rest the same.
    The player starts to slow down untill he stops.

    Maybe you dont have the friction on your player...

    In that case another suggestion can be:
    Set a default friction on all the plataforms (that aren't the speed up ones), like 0,5.
    To the speed up plataforms set a friction of 0,1 or 0.
    And apply a constant force on your player
    Then when he is on the default plataform he will go with the "normal velocity",on the speed up plataform he will go faster because he have less of no friction..

    I think u got the idea... Or maybe i didn't get yours :p
    Sorry if i misunderstood you.

    gl.
     
  8. sebi3110

    sebi3110

    Joined:
    Oct 11, 2012
    Posts:
    17
    Thank you Vitor for your huge post...
    So I made a little test scene with a similiar setup like you described...
    After a lot of testing the upshot was that I restricted the maximal speed in x direction somehwere at the beginning of my playercontrol script, which I had completly forgotten about :D
    After deleting this little line out of a sudden it worked completly for me....

    Thanks for your time!
     
  9. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    No problem man... i'm glad i could help..

    gl.