void FixedUpdate() { bool isOnGround = groundDetector.IsOnGround(); bool directionReversed = transform.localScale.x < 0; if (isOnGround) { var targetVelocity = new Vector2(Speed * (directionReversed ? -1 : 1), rigidbody2d.velocity.y); rigidbody2d.velocity = targetVelocity; } }
void FixedUpdate() { bool isOnGround = groundDetector.IsOnGround(); if (isOnGround) { CurrentJumpState = JumpState.ON_GROUND; } else if (CurrentJumpState != JumpState.JUMPING) { CurrentJumpState = JumpState.FALLING; } float horizontal = Input.GetAxis("Horizontal"); if (isOnGround) { rigidbody2d.AddForce(new Vector2(horizontal * Speed, 0)); } else { rigidbody2d.AddForce(new Vector2(horizontal * MidAirControlSpeed, 0)); } // Decelerate for better control if (isOnGround && Mathf.Approximately(horizontal, 0)) { rigidbody2d.AddForce(new Vector2(rigidbody2d.velocity.x * -Deceleration, 0)); } bool jumpButton = Input.GetAxis("Vertical") > 0; if (jumpButton) { if ((CurrentJumpState == JumpState.ON_GROUND || CurrentJumpState == JumpState.JUMPING) && jumpTime < MaxJumpTime) { CurrentJumpState = JumpState.JUMPING; rigidbody2d.AddForce(new Vector2(0, JumpForce)); jumpTime += Time.deltaTime; } } else if (CurrentJumpState == JumpState.ON_GROUND) { jumpTime = 0; } if (CurrentJumpState == JumpState.JUMPING && (jumpTime >= MaxJumpTime || !jumpButton)) { CurrentJumpState = JumpState.FALLING; } }
// Update is called once per frame void FixedUpdate() { float velocityX = rigidbody2d.velocity.x; bool isOnGround = groundDetector.IsOnGround(); if (velocityX != 0) { if (velocityX > 0) { transform.localScale = new Vector3(1, 1, 1); } else { transform.localScale = new Vector3(-1, 1, 1); } } if (!Mathf.Approximately(0, velocityX) && isOnGround) { if (currentAnimationClip != Walk) { currentAnimationClip = Walk; animator.Play(Walk.name); } animator.speed = Mathf.Abs(velocityX) / NormalWalkingSpeed; } else if (!isOnGround) { if (currentAnimationClip != Air) { currentAnimationClip = Air; animator.Play(Air.name); } } else { if (currentAnimationClip != Stand) { currentAnimationClip = Stand; animator.Play(Stand.name); } } }
void Update() { if (!UIController.gameStarted) { return; } grounded = groundDetector.IsOnGround(); if (grounded && !prevGrounded) { audioManager.Play("footstep"); autoClimbDown = true; } prevGrounded = grounded; if (grounded) { inAir = 0; jumped = false; } else { inAir++; } FindInteractType(); switch (state) { case States.Normal: ledgeBelow = ledgeDetector.DetectLedgeBelow(); if (grounded) { lastSafePosition = transform.position; if (input.runBtnHold) { speedFactor = Mathf.Lerp(speedFactor, 1.5f, 18 * Time.deltaTime); autoClimbDown = false; } else { speedFactor = Mathf.Lerp(speedFactor, 1, 18 * Time.deltaTime); autoClimbDown = true; } if (input.moveMagnitude > 0.1) { Vector3 targetDirection = input.goingForward * camera.forward + input.goingRight * camera.right; modelTransform.forward = Vector3.Slerp(modelTransform.forward, targetDirection, 24 * Time.deltaTime); } } else { //audioManager.Stop("walk"); if (autoClimbDown && ledgeBelow) { if (ledgeDetector.EnterLedge()) { SetStateGrab(); rb.velocity = new Vector3(0, 0, 0); camera.targetRotation.x = 70; input.LockInputForSeconds(0.5f); break; } } if (input.moveMagnitude > 0.1) { Vector3 targetDirection = input.goingForward * camera.forward + input.goingRight * camera.right; modelTransform.forward = Vector3.Slerp(modelTransform.forward, targetDirection, 6 * Time.deltaTime); } if (input.grabBtnDown) { animator.SetBool("on_ledge", true); hangCollider.TurnOnCollider(); grabArmCollider.TurnOnCollider(); grabStuckSecondChance = 5; // velocity becomes 0 once when jumping up state = States.Grab; } } if (input.jumpPressed && inAir < 6 && !jumpPending && !jumped) // left ground for less than 0.1 seconds { jumpPending = true; audioManager.Play($"jump{Random.Range(1, 3)}"); } if (input.interactBtnDown) { if (interact == InteractType.Ladder) { rb.useGravity = false; animator.SetBool("climb", true); currentLadder = hitInfo.collider.GetComponentInParent <Ladder>(); currentLadder.GetOntoLadder(transform, modelTransform, forwardSpeed); state = States.LadderEnter; } else if (interact == InteractType.Dialogue) { // trigger dialogue DialogueTrigger trigger = hitInfo.collider.GetComponentInParent <DialogueTrigger>(); trigger.TriggerDialogue(); rb.isKinematic = true; state = States.Dialogue; } else if (interact == InteractType.Checkpoint) { currentCheckpoint = hitInfo.collider.GetComponentInParent <CheckpointManager>(); currentCheckpoint.HandleInteract(this, dialogueManager); rb.isKinematic = true; state = States.Dialogue; } else if (interact == InteractType.Item) { hitInfo.collider.GetComponent <IItem>().PickUp(); } } break; case States.OnLadder: if (input.interactBtnDown) { autoClimbDown = false; SetStateNormal(); } if (transform.position.y >= currentLadder.TopY - 1.70f) { transform.position = new Vector3(transform.position.x, currentLadder.TopY - 1.65f, transform.position.z); state = States.LadderExit; animator.SetTrigger("ladder_top"); } else if (grounded) { SetStateNormal(); } break; case States.GrabStable: if (Mathf.Abs(rb.velocity.y) > 0.5f) { state = States.Grab; break; } ledgeDetector.AdjustFacingToLedge(out onTelephoneWire); if (input.runBtnHold) { speedFactor = Mathf.Lerp(speedFactor, 1.2f, 0.3f); } else { speedFactor = Mathf.Lerp(speedFactor, 1, 0.3f); } if (input.goingForward > 0) // teleport up //rb.isKinematic = true; //animator.SetTrigger("ledge_climb_up"); { ledgeDetector.ClimbUpLedge(onTelephoneWire); SetStateNormal(); camera.ResetVertical(); input.LockInputForSeconds(0.5f); } else if (input.goingForward < 0) { SetStateNormal(); audioManager.Play($"land{Random.Range(1, 3)}"); input.LockInputForSeconds(0.5f); } animator.SetFloat("ledge_speed", input.goingRight * speedFactor); break; case States.Grab: animator.SetFloat("ledge_speed", 0); break; case States.OnClimbGrid: if (input.runBtnHold) { climbController.speed_linear = Mathf.Lerp(climbController.speed_linear, climbGridSpeed * 1.5f, 18 * Time.deltaTime); } else { climbController.speed_linear = Mathf.Lerp(climbController.speed_linear, climbGridSpeed, 18 * Time.deltaTime); } break; case States.Dialogue: if (input.jumpPressed || Input.GetMouseButtonDown(0)) { dialogueManager.DisplayNextSentence(); } input.moveMagnitude = 0; break; } animator.SetFloat("ground_speed", input.moveMagnitude * speedFactor); animator.SetBool("grounded", grounded); animator.SetFloat("vertical_speed", rb.velocity.y); if (transform.position.y < -80) { currentCheckpoint.Respawn(this); } debugText.text = $"{state} {rb.velocity}"; /////////// CHEATS ///////////// if (Input.GetKeyDown(KeyCode.Keypad1)) { transform.position = new Vector3(-6.139141f, -18.53121f, -102.8717f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad2)) { transform.position = new Vector3(110.465f, -13.794f, -282.967f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad3)) { transform.position = new Vector3(239.36f, -32.12f, -393.23f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad4)) { transform.position = new Vector3(319.6365f, -16.09863f, -437.5951f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad5)) { transform.position = new Vector3(468.11f, -3.24f, -451.02f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad6)) { transform.position = new Vector3(498.3731f, -1.98288f, -475.4491f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad7)) { transform.position = new Vector3(532.3205f, -7.150647f, -504.4659f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad8)) { transform.position = new Vector3(564.0289f, -17.41118f, -551.7151f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.Keypad9)) { transform.position = new Vector3(574.7895f, -20.05804f, -564.5358f); SetStateNormal(); } else if (Input.GetKeyDown(KeyCode.KeypadMultiply)) { var mmc = FindObjectOfType <MusicMenuController>(); for (int i = 0; i < mmc.musicItems.Length; i++) { if (!mmc.musicItems[i].collected) { mmc.AddToCollection(i); break; } } } else if (Input.GetKeyDown(KeyCode.KeypadPlus)) { livesMax++; } else if (Input.GetKeyDown(KeyCode.KeypadMinus)) { livesMax--; } else if (Input.GetKeyDown(KeyCode.KeypadPeriod)) { lives = 9999999; } }