# How do I stop a velocity only on the X axis?

I am making a character that moves in a 3D world with 2D mechanics.
They slip and slide about using the code below. I want it so that when the player lets go of the A/D keys it quickly brings them to a stop on the X-axis. I don’t want to use dampen on the Rigidbody because that will affect both Jumping and falling.

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

public class PlayerMove : MonoBehaviour
{
public float forceMult = 100;
public float GravityMult = 0.5f; // Gravity
private Rigidbody rb; //Rigidbody

public bool JumpReady = true; //Jump only if on floor

void Start()
{
rb = GetComponent<Rigidbody>();
}

// Update is called once per frame
void Update()
{
//Movement

//Jump
if (Input.GetKey(KeyCode.W))rb.AddForce(Vector3.up * forceMult / 2);
if (Input.GetKeyDown(KeyCode.W) && JumpReady == true)
{
}

//DashDown
if (Input.GetKey(KeyCode.S))rb.AddForce(Vector3.down * forceMult * 15);

//Movement End
if (Input.GetKeyUp(KeyCode.A))
{
//Quick Stop A
}
if (Input.GetKeyUp(KeyCode.D))
{
//Quick Stop D
}

//Gravity
rb.AddForce(Vector3.down * forceMult * GravityMult * 2);
}
}
``````

Rigidbodys have a property velocity, see more about it here:

You can assing a new velocity, Vector3, with the x coordinate = 0:

``````rb.velocity = new Vector3(0f, rb.velocity.y, rb.velocity.z);
``````

This should do the trick.

You can use rigidbody constraints in the inspector under rigidbody component. There just check " Freeze Position X"