Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Q: Maintaining constant velocity when jumping onto a sloped collider

Discussion in 'Flash' started by RisingNebula, Jul 18, 2014.

  1. RisingNebula

    RisingNebula

    Joined:
    Aug 11, 2013
    Posts:
    5
    Hello, I am making a 2d endless runner where the player runs automatically at a constant speed.
    I am however having difficulties maintaining a constant speed when the player jumps onto a sloped/curved surface. I have added a surface material with 0 friction, which has helped. But there is still a noticable slow down when landing on the surface. (I have a circle collider at the players feet)

    Is there any way to keep a constant speed no matter what the angle of the floor is?

    The code i am using to move my player object is:
    Code (CSharp):
    1. void FixedUpdate()
    2. {
    3. rigidbody2D.velocity = new Vector2(maxSpeed, rigidbody2D.velocity.y);
    4. }
     
  2. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Try implementing"OnCollisionEnter2D" and set the velocity there after the collision.

    If that still leaves a noticable impact moment, I'd switch to "isKinematic" on collision and start a co-routine that fakes the physics for a bit before switching back to actual physics.
     
  3. RisingNebula

    RisingNebula

    Joined:
    Aug 11, 2013
    Posts:
    5
    Thanks for the quick reply. I tried your advice to make a co-routine, however the results are a little strange. When StopCoroutine is called and the player is still on the slope, it starts to jerk about. As i have a undefined variety of curved colliers, faking physics could be tricky. Any more suggestion i could try?
     
  4. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    Have you tried the first suggestion? In OnCollisionEnter2D, you should get the normal vector of the edge you're hitting. I'd use that to calculate and manually set the new velocity vector. Get a perpendicular vector to that normal and scale by the speed you want. Hope that helps!