I have a static variable of ‘isCrouching’ and a script which allows the player to run:
//Run.js
var character : CharacterController = GetComponent(CharacterController);
if (!InGameMenu.isPaused) {
if (Input.GetKey(KeyCode.LeftShift)) {
Crouch.isCrouching = false;
character.SimpleMove(transform.TransformDirection (Vector3.forward) * maxSpeed);
audio.clip = runningSound;
audio.Play();
isRunning = true;
isCrouching should be disabled when the running function begins.
//Crouch.js
if (!Run.isRunning && Input.GetKeyDown(KeyCode.C)) {
isCrouching = !isCrouching;
if (isCrouching) {
playerMesh.transform.localScale = Vector3(1, 0.75, 1);
playerMesh.transform.localPosition = Vector3(0, -0.25, 0);
mainCam.transform.localPosition = Vector3(0, 0, 0);
character.height = 1.5;
character.center = Vector3(0, -0.25, 0);
}
if (!isCrouching) {
playerMesh.transform.localScale = Vector3(1, 1, 1);
playerMesh.transform.localPosition = Vector3(0, 0, 0);
mainCam.transform.localPosition = Vector3(0, 0.5, 0);
character.height = 2;
character.center = Vector3(0, 0, 0);
}
isCrouching cannot be activated while ‘isRunning’ is. I have no idea why, but when ‘isRunning’ is activated, ‘isCrouching’ is not deactivated. Any tips?