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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Rigidbody2D movement problems

Discussion in 'Editor & General Support' started by ripefruits22, Mar 10, 2022.

  1. ripefruits22

    ripefruits22

    Joined:
    Nov 26, 2021
    Posts:
    17
    I've made a movement script that uses a rigidbody2d component to move, and so far everything has been working fine, except for changing directions. I tried to implement the movement to have an acceleration as opposed to having the movement be instant, but I've noticed that whenever the player tries to change directions on the ground, the player will just stop moving for a short period of time. However the movement works completely fine in the air, and does exactly what I want it to do so it shouldn't be a problem with the script:
    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     [SerializeField] private KeyCode left;
    4.     [SerializeField] private KeyCode right;
    5.     [SerializeField] private KeyCode jump;
    6.     [SerializeField] private float gravity;
    7.     [SerializeField] private float speed;
    8.     [SerializeField] private float jumpForce;
    9.     [SerializeField] private float inertia;
    10.     [SerializeField] private LayerMask layer;
    11.     [SerializeField] private Transform groundPoint;
    12.  
    13.     private Vector2 moveVector = Vector2.zero;
    14.  
    15.     private Rigidbody2D rb;
    16.  
    17.     private float xMove;
    18.     private float yMove;
    19.     private float jumpRememberTimer;
    20.     private float jumpRememberTime = 0.1f;
    21.  
    22.     private bool isGrounded;
    23.     private bool hasJumped;
    24.  
    25.     private void Start()
    26.     {
    27.         rb = GetComponent<Rigidbody2D>();
    28.         Physics2D.gravity = Vector2.zero;
    29.     }
    30.  
    31.     private void Update()
    32.     {
    33.         isGrounded = Physics2D.OverlapBox(groundPoint.position, new Vector2(1, 0.1f), 0, layer);
    34.         Debug.Log(isGrounded);
    35.         //horizontal movement
    36.         if ((Input.GetKey(left) && !Input.GetKey(right)))
    37.         {
    38.             xMove -= speed * Time.deltaTime * inertia;
    39.         }
    40.         if ((Input.GetKey(right) && !Input.GetKey(left)))
    41.         {
    42.             xMove += speed * Time.deltaTime * inertia;
    43.         }
    44.         if (!Input.GetKey(left) && !Input.GetKey(right))
    45.         {
    46.             xMove -= moveVector.x * Time.deltaTime * inertia;
    47.         }
    48.  
    49.         //speedchecks
    50.         if (xMove > speed)
    51.         {
    52.             xMove = speed;
    53.         } else if ( xMove < -speed)
    54.         {
    55.             xMove = -speed;
    56.         }
    57.  
    58.         moveVector.x = xMove;
    59.  
    60.  
    61.         //jumping
    62.         if(Input.GetKeyDown(jump) && !hasJumped && isGrounded)
    63.         {
    64.             yMove = jumpForce;
    65.             hasJumped = true;
    66.             jumpRememberTimer = 0;
    67.         }
    68.  
    69.         //resetting has jumped
    70.         jumpRememberTimer += Time.deltaTime;
    71.         if(jumpRememberTimer > jumpRememberTime && isGrounded)
    72.         {
    73.             hasJumped = false;
    74.         }
    75.  
    76.         //gravity
    77.         yMove -= gravity * Time.deltaTime;
    78.         if (yMove < -10f && isGrounded)
    79.         {
    80.             yMove = -10f;
    81.         }
    82.  
    83.         //assign values
    84.         moveVector = new Vector2(xMove, yMove);
    85.        
    86.         //applying the movevector
    87.         rb.velocity = moveVector;
    88.     }
    89. }
    I have also noticed that as I increase the inertia value, the time that the player freezes shortens.
     
  2. ripefruits22

    ripefruits22

    Joined:
    Nov 26, 2021
    Posts:
    17
    I solved the problem by making a new physics material with 0 friction.