How to make addRelativeForce not slide?

Hi, I am trying to make an FPS controller and am currently trying to add movement. I am using addRelativeForce but it pretty much just slides all over the place. I tried doing this:

if(Physics.Raycast(transform.position, Vector3.down, 1))
		{
			if((Input.GetAxisRaw("Horizontal") != 0) || (Input.GetAxisRaw("Vertical") != 0))
			{
				rigidbody.drag = 0;
			}else
			{
				rigidbody.drag = 10;
			}
		}else
		{
		rigidbody.drag = 0;	
		}

However, this just makes it so you stop correctly after letting go of the keys. While you are moving, you still slide, so I need another way of doing this. The full code I use is this:

using UnityEngine;
using System.Collections;

public class PlayerMovementScript : MonoBehaviour 
{
	public CameraScript cameraScript;
	private float cameraRotationY;
	
	private float movementForceVertical;
	private float movementForceHorizontal;
	
	public float movementSpeedHorizontal = 20f * 50;
	public float movementSpeedVertical = 30f * 50;
	
	
	void Start () 
	{
	}
	
	void Update () 
	{
		cameraRotationY = cameraScript.getRotation();
		transform.rotation = Quaternion.Euler(0,cameraRotationY,0);
		
		
		movementForceHorizontal = Input.GetAxisRaw("Horizontal") * movementSpeedHorizontal;
		movementForceVertical = Input.GetAxisRaw("Vertical") * movementSpeedVertical;
		
		if(Physics.Raycast(transform.position, Vector3.down, 1))
		{
			if((Input.GetAxisRaw("Horizontal") != 0) || (Input.GetAxisRaw("Vertical") != 0))
			{
				rigidbody.drag = 0;
			}else
			{
				rigidbody.drag = 10;
			}
		}else
		{
		rigidbody.drag = 0;	
		}
		
	}
	
	void FixedUpdate()
	{
		rigidbody.AddRelativeForce(movementForceHorizontal, 0, movementForceVertical);
		
	}
}

There is probably a way more efficient way of doing some of the things, but I am still learning C#. Thanks!

Often solving these problems takes a very explicit definition of the behavior by you. There are multiple ways of not sliding, but the user experience will be different:

  1. Set a higher drag in the Rigidbody component. The higher the drag, the less sliding. Of course you will need to increase the force applied each frame.

  2. Redirect the velocity:

    void FixedUpdate()
    {
    Vector3 dir = new Vector3(movementForceHorizontal, 0, movementForceVertical);
    rigidbody.velocity = dir.normalized * rigidbody.velocity.magnitude;
    rigidbody.AddRelativeForce(dir);
    }

  3. Project the velocity onto the new direction:

    void FixedUpdate()
    {
    Vector3 dir = new Vector3(movementForceHorizontal, 0, movementForceVertical);
    rigidbody.velocity = Vector3.Project(rigidbody.velocity, dir.normalized);
    rigidbody.AddRelativeForce(dir);
    }