// Use this for initialization void Start() { currentMotion = motionstate.idle; moveDirection = Vector3.zero; grounded = false; playerControl = false; controller = GetComponent <CharacterController>(); myTransform = transform; speed = walkSpeed; crosshairScript = transform.Find("FPSCamera").GetComponent <Crosshair>(); // Lock cursor Cursor.visible = false; }
private void FixedUpdate() { float inputX = Input.GetAxis("Horizontal"); float inputY = Input.GetAxis("Vertical"); float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && limitDiagonalSpeed) ? 0.6701f : 1.0f; if (inputX == 0 && inputY == 0) { handsAnim.SetBool("idle", true); handsAnim.SetBool("running", false); currentMotion = motionstate.idle; } if (grounded) { if (falling) { falling = false; if (myTransform.position.y < (fallStartLevel - fallingDamageLimit)) { FallingDamageAlert(fallStartLevel - myTransform.position.y); } } if (!toggleRun) { bool running = Input.GetButton("Run"); speed = running ? runSpeed : walkSpeed; handsAnim.SetBool("running", running); handsAnim.SetBool("idle", false); if (running) { currentMotion = motionstate.running; crosshairScript.IncreaseSpread(0.5f); } else { if (crosshairScript.spread != crosshairScript.minSpread) { crosshairScript.DecreaseSpread(2f); } } } if (Input.GetButtonUp("Run")) { currentMotion = PlayerController.motionstate.idle; handsAnim.SetBool("running", false); handsAnim.SetBool("idle", true); } if (!toggleSneak) { bool sneaking = Input.GetButton("Sneak"); speed = sneaking ? sneakSpeed : speed; //anim.SetBool("sneaking", sneaking); } if (crouching) { speed = Input.GetButton("Run") ? crouchRunSpeed : crouchWalkSpeed; speed = Input.GetButton("Sneak") ? crouchSneakSpeed : speed; } moveDirection = new Vector3(inputX * inputModifyFactor, 0, inputY * inputModifyFactor); moveDirection = myTransform.TransformDirection(moveDirection) * speed; if (!Input.GetButton("Jump")) { //anim.SetBool("Jump", false); } else { moveDirection.y = jumpSpeed; crosshairScript.IncreaseSpread(10f); currentMotion = motionstate.jumping; } } else { if (!falling) { falling = true; fallStartLevel = myTransform.position.y; } if (airControl && playerControl) { moveDirection.x = inputX * speed * inputModifyFactor; moveDirection.z = inputY * speed * inputModifyFactor; moveDirection = myTransform.TransformDirection(moveDirection); } } grounded = (controller.Move(moveDirection * Time.deltaTime) & CollisionFlags.Below) != 0; moveDirection.y -= gravity * Time.deltaTime; }