Rigidbody Z-Axis movement troubles.

Im making a roguelike game in the style of enter the gungeon and binding of isaac, and the way im trying to move my character is using Rigidbody’s velocity. It works normally for the x axis, instead of stopping immediately, when you let go of the key to move the character starts to slide to a stop. On the Z axis though, you stop immediately after you let go of the movement key, would anyone be able to look at my script and help me out? A little background, The Y axis isn’t being used at all, the X axis is Horizontal Movement, and the Z axis is Vertical movements. For the input manager, “Horizontal” has a positive of D and a negative of A. And “Z” has a positive of W and a negative of S.

Here’s the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VelocityTest : MonoBehaviour {
    public Rigidbody rb;
    private Vector3 movement;
    public Vector3 speed;


	// Use this for initialization
	void Start () {
        rb = GetComponent<Rigidbody>();
        
		
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        float InputX = Input.GetAxis("Horizontal");
        float InputZ = Input.GetAxis("Z");
        movement = new Vector3(
            speed.x * InputX,
            0,
            speed.z * InputZ);

        rb.velocity = movement;    


	

    }

}

Input.GetAxis ("Horizontal") is a method that outputs a value between -1 and 1 depending on which key you press. The special feature of this method is, that it does not directly output -1 or 1, but that the value increases or decreases linearly until it reaches 1 or -1. If you let go of all keys then the value approaches 0 linearly. This is the reason for this smooth braking. Since I do not find Input.GetAxis ("Z") in the documentation, I assume that you have added this axis under Edit > Project Settings > Input. The problem now is that this axis does not support smooth transitions between the values ​​and therefore outputs -1, 0 or 1 directly. You can reproduce the problem on the horizontal axis using Input.GetAxisRaw ("Horizontal").

Solution:

Since I assume that your game is played from the top-down view, you can use Input.GetAxis ("Vertical") for the z-axis. If you need this for a different behavior, Rigidbody.AddForce() allows you to calculate the velocity by the engine. But then I would recommend you to work with GetAxisRaw.