# Why is my Player sliding in one direction?

So if I press left shift then my Player slides to one direction. but if I look all up then it goes to x and when i look all the way down then it goes to z axis. Any ideas why?

Script:

``````	if (Input.GetKey (KeyCode.LeftShift)) {
canSlide = true;
GotTheLastLook = true;
}

if (GotTheLastLook) {
f = fpsCam.localRotation.x;
GotTheLastLook = false;
}

if (canSlide) {
SlideKeyPressed = true;
Sliding = true;
canSlide = true;
}

if (SlideKeyPressed) {
SlidingTime -= Time.deltaTime;
}

if (Sliding) {
transform.localScale = new Vector3 (1, slidingScale, 1);
canSlide = false;
}

if (SlidingTime <= 0.9f) {
rb.AddForce (new Vector3(0, 0, f * Time.deltaTime * slidingSpeed));
WalkingAllowed = false;
}

if (SlidingTime <= 0f) {
transform.localScale = new Vector3 (1, normalScale, 1);
SlidingTime = 1.0f;
Sliding = false;
SlideKeyPressed = false;
canSlide = false;
if (rb.velocity.magnitude > .01) {
rb.velocity = Vector3.zero;
}
}

if (SlidingTime >= 1) {
WalkingAllowed = true;
}
//---------------------------------------------------------------------
``````

The problem here is on line 27:

``````rb.AddForce (new Vector3(0, 0, f * Time.deltaTime * slidingSpeed));
``````

&nbsp

What’s happening here is that AddForce is applying a force of some magnitude along the global z-axis (so the world forwards / backwards). This is why it appears to be correct when facing certain directions. The ‘f’ variable is also a problem as it’s being set based on the tranform’s x-rotation (i.e. the camera’s pitch (or up / down rotation) in the case of the FPS camera). I’d just get rid of it completely.
&nbsp

What I think you actually want to do is apply the force along the camera’s local axis. You can get this from the camera’s transform like so:

``````rb.AddForce(fpsCam.transform.forward * Time.deltaTime * slidingSpeed);
``````

&nbsp

Hopefully this will give you the desired behaviour or at least get you on the right track
&nbsp

Note: I’m assuming it’s the z-axis you want to move along. You can change to ‘right’ for the x-axis or even ‘up’ for the y-axis.