Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Descent from the mountain

Discussion in 'Physics' started by ignarmezh, Aug 19, 2018.

  1. ignarmezh

    ignarmezh

    Joined:
    Mar 23, 2017
    Posts:
    53
    Hello.
    I'm trying to implement the physics of the game Ski Safari and Alto's Adventure.
    I can not make a move from the mountain.
    My player has a RigidBody2D
    upload_2018-8-20_0-55-8.png
    and a PolygonCollider2d for skiing(about standard settings). The floor has the Edge Collider 2D.
    It is possible to make the floor perfectly smooth, but it is rather difficult if the number of floors is large.
    The problem is that the movement in these games is very smooth, there the player seems to stick to the floor. In my case, the player shakes a little when moving, what do you advise? Maybe I have an extremely wrong approach?
    You can download unitypackage with my project(Scene: SampleScene).


    Code (CSharp):
    1. public class Player : MonoBehaviour {
    2.  
    3.     [SerializeField]
    4.     private float speed;
    5.     [SerializeField]
    6.     private float impulseAddForce;
    7.  
    8.     public bool isGrounded;
    9.  
    10.     private Rigidbody2D playerRB;
    11.  
    12.     private void Awake()
    13.     {
    14.         playerRB = GetComponent<Rigidbody2D>();
    15.         isGrounded = false;
    16.     }
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         if(playerRB.velocity.magnitude < speed && isGrounded)
    21.         {
    22.             playerRB.AddForce(new Vector2(1,-1) * impulseAddForce,ForceMode2D.Impulse);
    23.         }
    24.     }
    25.  
    26.     private void OnCollisionStay2D(Collision2D collision)
    27.     {
    28.         if(collision.transform.tag == "Ground")
    29.         {
    30.             isGrounded = true;
    31.         }
    32.     }
    33.  
    34.     private void OnCollisionExit2D(Collision2D collision)
    35.     {
    36.         isGrounded = false;
    37.     }
    38. }
     

    Attached Files:

  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,558
    Did you try apply physics material to the contact surface?
     
  3. ignarmezh

    ignarmezh

    Joined:
    Mar 23, 2017
    Posts:
    53
    With Material Physics 2D looks better.
    But there is no sticking effect, that is, when the speed rises (not significantly), the player tears off the surface, maybe it's worth playing around with the settings(gravity, mass)? And also there were jerks when trying to increase the speed of the player, if speed rather small, I think it's because of the vector in AddFors
    Code (CSharp):
    1. playerRB.AddForce(gameObject.transforn.right * impulseAddForce,ForceMode2D.Impulse);
    , but how to fix it?
     
    Last edited: Aug 20, 2018