Home > Back-end >  How can I reduce my character from sliding?
How can I reduce my character from sliding?

Time:02-25

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
public float speed;
public float jump;

private Rigidbody2D rb;

private void Start()
{
    rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
    rb.position  = new Vector2(Input.GetAxis("Horizontal"), 0) * Time.deltaTime * speed;
    if(Mathf.Abs(rb.velocity.y) < 0.001f && Input.GetKeyDown(KeyCode.W))
    {
        rb.AddForce(new Vector2(0, jump), ForceMode2D.Impulse);
    }
}

So I have this code for my player movement. I am wondering how can I reduce my character from sliding that much. I don't want to stop instantly after I release the key.

CodePudding user response:

Inside of the Input Manager, Edit->Project Settings->Input Manager, there is a property called gravity.

enter image description here

Gravity: Speed in units per second that the axis falls toward neutral when no input is present.

Decreasing this value will cause the input to fall quicker, resulting in less/no sliding.

You can debug your input value to confirm this. You should notice a ramp up from 0 to 1/-1 when you first hold the horizontal input. Once you let go of the input, you should see the value fall back down to 0.

var inputHorz = Input.GetAxis("Horizontal");

Debug.Log(inputHorz);

Lower the value until it feel correct. This can be changed while you are playing the game, but you will need to paste that value back in after pressing stop.

CodePudding user response:

You could add counter-movement to make the movement to feel more responsive, or you could change the friction by adding a physics material. Counter-movement makes the player stop by adding a force opposite to the wanted direction of the movement. It will stop the player and stop it from sliding to much. Another approach is to add a physics material and up the friction a bit. This will make the player stop faster. I hope you find this helpful!

  • Related