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;
}
}