//Handle physics void FixedUpdate() { if (TheGame.IsGamePaused()) { return; } PlayerControls controls = PlayerControls.Get(player_id); //Movement velocity Vector3 move_input = controls.GetMove(); float desiredSpeed = Mathf.Abs(move_input.x) > 0.1f ? move_input.x * move_max : 0f; float acceleration = Mathf.Abs(move_input.x) > 0.1f ? move_accel : move_deccel; acceleration = !is_grounded ? jump_move_percent * acceleration : acceleration; move.x = Mathf.MoveTowards(move.x, desiredSpeed, acceleration * Time.fixedDeltaTime); was_grounded = is_grounded; is_grounded = DetectObstacle(Vector3.down); is_ceiled = DetectObstacle(Vector3.up); is_fronted = IsFronted(); if (state == PlayerCharacterState.Normal) { UpdateFacing(); UpdateJump(); UpdateCrouch(); //Move move.x = is_fronted ? 0f : move.x; rigid.velocity = move; CheckForFloorTrigger(); } if (state == PlayerCharacterState.Climb) { move = controls.GetMove() * climb_speed; rigid.velocity = move; } if (state == PlayerCharacterState.Dead) { move.x = 0f; UpdateJump(); //Keep falling rigid.velocity = move; } }
//Handle render and controls void Update() { if (is_dead) { return; } hit_timer += Time.deltaTime; grounded_timer += Time.deltaTime; //Controls PlayerControls controls = PlayerControls.Get(player_id); move_input = !disable_controls?controls.GetMove() : Vector2.zero; jump_press = !disable_controls?controls.GetJumpDown() : false; jump_hold = !disable_controls?controls.GetJumpHold() : false; if (jump_press || move_input.y > 0.5f) { Jump(); } //Reset when fall if (transform.position.y < fall_pos_y - GetSize().y) { TakeDamage(max_hp * fall_damage_percent); if (reset_when_fall) { Teleport(last_ground_pos); } } }
private void UpdateCrouch() { if (!can_crouch) { return; } PlayerControls controls = PlayerControls.Get(player_id); //Crouch bool was_crouch = is_crouch; if (controls.GetMove().y < -0.1f && is_grounded) { is_crouch = true; move = Vector2.zero; capsule_coll.size = new Vector2(coll_start_h.x, coll_start_h.y * crouch_coll_percent); capsule_coll.offset = new Vector2(coll_start_off.x, coll_start_off.y - coll_start_h.y * (1f - crouch_coll_percent) / 2f); if (!was_crouch && is_crouch) { if (onCrouch != null) { onCrouch.Invoke(); } } } else { is_crouch = false; capsule_coll.size = coll_start_h; capsule_coll.offset = coll_start_off; } }
//Handle render and controls void Update() { if (TheGame.IsGamePaused()) { return; } hit_timer += Time.deltaTime; state_timer += Time.deltaTime; //grounded_timer += Time.deltaTime; //Controls PlayerControls controls = PlayerControls.Get(player_id); if (state == PlayerCharacterState.Normal) { if (controls.GetJumpDown()) { Jump(); } Ladder ladder = Ladder.GetOverlapLadder(gameObject); if (ladder && controls.GetMove().y > 0.1f && state_timer > 0.7f) { Climb(); } } if (state == PlayerCharacterState.Climb) { Ladder ladder = Ladder.GetOverlapLadder(gameObject); if (ladder == null) { state = PlayerCharacterState.Normal; state_timer = 0f; } if (controls.GetJumpDown()) { Jump(true); } } //Reset when fall if (!IsDead() && transform.position.y < fall_pos_y - GetSize().y) { TakeDamage(fall_damage); if (reset_when_fall) { Teleport(last_ground_pos); } } }
//Handle render and controls void Update() { if (is_dead) { return; } hit_timer += Time.deltaTime; grounded_timer += Time.deltaTime; //Controls PlayerControls controls = PlayerControls.Get(player_id); move_input = !disable_controls?controls.GetMove() : Vector2.zero; jump_press = !disable_controls?controls.GetJumpDown() : false; jump_hold = !disable_controls?controls.GetJumpHold() : false; if (jump_press || move_input.y > 0.5f) { Jump(); } //Reset when fall if (transform.position.y < fall_pos_y - GetSize().y) { TakeDamage(max_hp * fall_damage_percent); if (reset_when_fall) { Teleport(last_ground_pos); } } if (grayness < 1 && ( (rigid.position.x >= 20 && rigid.position.x <= 24 && rigid.position.y > -1 && rigid.position.y < 0) || (rigid.position.x >= 39 && rigid.position.x <= 41 && rigid.position.y > 7 && rigid.position.y < 8) || (rigid.position.x >= 54 && rigid.position.x <= 56 && rigid.position.y > 22 && rigid.position.y < 23) || (rigid.position.x >= 80 && rigid.position.y > -3 && rigid.position.y < -2) )) { grayness += 0.01f; } else if (grayness > 0) { grayness -= 0.001f; } character.GetComponent <SpriteRenderer>().color = new Color(grayness, grayness, grayness, 1); }