- Home /
Sprint and Crouch problem!
Hey, so I made an FPS movement and mouselook script and now added sprinting and crouching. The problem is that if I once entered sprinting or crouching I can't return to the normal walk speed because if I use: else { currentspeed = speed } it will keep setting currentspeed to speed if the Shift button is released but I want it to just once set it back to the walk speed and not all the time. Any ideas?
Sorry for my terrible English!
Here's my code:
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public float speed;
public float runSpeed;
public float crouchSpeed;
public float currentSpeed;
public float sensitivity = 2f;
CharacterController player;
public GameObject playerCamera;
float moveFB;
float moveLR;
float rotX;
float rotY;
void Start()
{
player = GetComponent<CharacterController>();
currentSpeed = speed;
}
void Update()
{
Cursor.lockState = CursorLockMode.Locked;
moveFB = Input.GetAxis("Vertical") * currentSpeed;
moveLR = Input.GetAxis("Horizontal") * currentSpeed;
rotY = Input.GetAxis("Mouse X") * sensitivity;
rotX -= Input.GetAxis("Mouse Y") * sensitivity;
rotX = Mathf.Clamp(rotX, -90f, 90f);
Vector3 movement = new Vector3(moveLR, 0, moveFB);
transform.Rotate(0, rotY, 0);
playerCamera.transform.localRotation = Quaternion.Euler(rotX, 0, 0);
//sprinting
if (Input.GetKey(KeyCode.LeftShift) && GameObject.Find("WeaponManager").GetComponent<WeaponManagerAnimator>().isAiming != true)
{
currentSpeed = runSpeed;
}
else
{
currentSpeed = speed;
}
movement = transform.rotation * movement;
player.Move(movement * Time.deltaTime);
//gravity
moveDirection.y -= gravity * Time.deltaTime;
player.Move(moveDirection * Time.deltaTime);
//crouching
if (Input.GetKeyDown(KeyCode.X))
{
currentSpeed = crouchSpeed;
}
else
{
currentSpeed = speed;
}
}
Fixed it! Just added this line of code:
if (isRunning == false && isCrouching == false) currentSpeed = speed;
Answer by Hinkel · Feb 27, 2017 at 03:04 PM
Fixed it! Just added this line of code:
if (isRunning == false && isCrouching == false) currentSpeed = speed;
Your answer
Follow this Question
Related Questions
Player Falls Much Slower While Moving 2 Answers
MY FPSE`S fps is not moving need help! I tried all solutions! 0 Answers
Why is the Camera bouncing on the X-Axis?,Why does the Camera jump around on the X-Axis? 1 Answer
why is the mouse look messing up on the new fps character 0 Answers
How to create a stationary weapon FPS controller ? 2 Answers