Movement with AddForce: Wrong Direction

Hey everyone; I have a Player Controller script that is supposed to apply a force to the player GameObject to make it move in a direction based upon user input.

public void MoveUpdate () {
		float h = Input.GetAxis("Horizontal");
		float v = Input.GetAxis("Vertical");
		if(h * this.rigidbody.velocity.x < maxSpeed) {
			this.rigidbody.AddForce(Vector3.right * h * moveForce);
		}
		if(Mathf.Abs(this.rigidbody.velocity.x) > maxSpeed) {
			this.rigidbody.velocity = new Vector3(Mathf.Sign(this.rigidbody.velocity.x) * maxSpeed, this.rigidbody.velocity.y, this.rigidbody.velocity.z);
		}
		if(v * this.rigidbody.velocity.z < maxSpeed) {
			this.rigidbody.AddForce(Vector3.forward * v * moveForce);
		}
		if(Mathf.Abs(this.rigidbody.velocity.z) > maxSpeed) {
			this.rigidbody.velocity = new Vector3(this.rigidbody.velocity.x, this.rigidbody.velocity.y, Mathf.Sign(this.rigidbody.velocity.z) * maxSpeed);
		}
		if(h > 0 && !facingRight) {
			Flip();
		}
		else if(h < 0 && facingRight) {
			Flip(); 
		}
	}

When I press “w” or “up,” the forward input buttons (a.k.a. Input.GetAxis(“Vertical”)), the player also moves a little bit to the left or right, though. Why might this be happening? I would like the player to only move forward if only the “w” or “up” inputs are activated.

Edit: I’ve verified that the “h” value is never anything higher or lower than zero when this happens, and I’ve tested this script on a basic cube object, with the same results.

Edit #2: I’ve discovered that the issue is actually because I am using more than one collider to create a compound collider; when turning off one, it works perfectly. What is the best way to get around this? I need multiple colliders to match the shape of my character correctly.

Your issue is more than likely due to physics drag. Try going to your projects Physics settings and switch the material to a super smooth Physics material (you can pull it out of the Physics material Asset Package). If your odd movement is fixed, you should consider using a Capsule or Sphere collision box (they have less surface area and will create less drag), instead of a cube, or use the default smooth physics material.

The best way that I fould to have compond colliders on objects withou causing Physics issues is to set their collision properties so that they are unable to collide with themselves.
The biggest issue with this will be that clones of objects setup like this wont collide with each other.