- Home /
Problem is not reproducible or outdated
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;
}
//---------------------------------------------------------------------
Couple of questions:
Can your player look to the left and right as well as up and down (like in a regular FPS)?
What type of object is fpsCam? (I'm guessing it's a Transform?)
@alwayscodeangry also when i looked all the way down x rotation was 100 then the slide was pretty smooth now i clamped it to 90 now it isnt so smooth. Like if i look everywhere the slide is bad but when its x rotation is 100 the slide gets smooth
Answer by alwayscodeangry · Feb 23, 2020 at 12:45 AM
The problem here is on line 27:
rb.AddForce (new Vector3(0, 0, f * Time.deltaTime * slidingSpeed));
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.
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);
Hopefully this will give you the desired behaviour or at least get you on the right track :)
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.
Yes i want to addforce to forward. but also in the last direction like in cod. So you press the slide button and then you slide in the last look rotation.
@alwayscodeangry How can I get the last look rotation? Like I tried with Vectors and Quaternions and i still cant get it i made so many scripts and nothing works.
I'm afraid I don't know what you mean by 'last look rotation'. Could you clarify?
@alwayscodeangry So lets say your y rotation is 20 and if you press shift to slide then you addforce to 20(the y rotation) and while you are sliding you can look everywhere but still sliding to the rotation of 20. but only y rotation (left and right).
Follow this Question
Related Questions
Does anyone know how to create sliding movement? 1 Answer
Help with Rotating code? 2 Answers
Move gameObject with mouse 1 Answer
I need help with my audio slider 0 Answers
2D Collider Transform for Sliding 1 Answer