private void Update() { /***********************************************************************************************************/ // Touch & Keyboard Input TouchPlot.SwipeDirection swipe = plot.GetSwipeDirection(25f); bool pressingLeft = Input.GetKeyDown(KeyCode.LeftArrow) || swipe == TouchPlot.SwipeDirection.LEFT, pressingRight = Input.GetKeyDown(KeyCode.RightArrow) || swipe == TouchPlot.SwipeDirection.RIGHT, pressingUp = Input.GetKeyDown(KeyCode.Space) || swipe == TouchPlot.SwipeDirection.UP, pressingDown = Input.GetKeyDown(KeyCode.DownArrow) || swipe == TouchPlot.SwipeDirection.DOWN; /************************************************************************************************************/ // Lane movement if (pressingLeft) { animator.SetTrigger("Left"); moveToLane(currentLane - 1); } else if (pressingRight) { animator.SetTrigger("Right"); moveToLane(currentLane + 1); } //Save rotation to override animator enforcedRotation = transform.rotation; /***************************************************************************************************************/ // Jumping & gravity calculation if (isGrounded) { // On the ground: if (!isJumping) { CameraReference.position = new Vector3( CameraReference.position.x, Mathf.Max(transform.position.y, 0), CameraReference.position.z ); animator.SetTrigger("Ground"); } isJumping = false; //Jumping if (pressingUp) { animator.ResetTrigger("Ground"); verticalVelocity = JumpMagnitudeFactor; slidingTimer = 0f; slideFromAir = false; isJumping = true; animator.SetTrigger("Jump"); } else { //Sliding if (pressingDown || slideFromAir) { slidingTimer = SlideTime; slideFromAir = false; enforcedRotation = Quaternion.identity; animator.SetTrigger("Slide"); } verticalVelocity = Mathf.Max(0, verticalVelocity); } // if (!isJumping && !isSliding) ; // enforcedRotation.Set(0f, enforcedRotation.y, enforcedRotation.z, enforcedRotation.w); } else { // In the air: animator.ResetTrigger("Left"); animator.ResetTrigger("Right"); if (pressingDown) { verticalVelocity = -4f * JumpMagnitudeFactor; slideFromAir = true; } else { verticalVelocity -= GravityMagnitudeFactor * Time.deltaTime; } } }
// Update is called once per frame void Update() { isGround = isGrounded(); RaycastHit hit; if (Physics.Raycast(new Ray( new Vector3(player.bounds.center.x, player.bounds.center.y - player.bounds.extents.y + 0.001f, player.bounds.center.z + player.bounds.extents.z - 0.2f), Vector3.forward), out hit, player.bounds.extents.z + 2.0f)) { if (!hit.collider.gameObject.name.Equals("End")) { isGround = true; float ang = (Vector3.Angle(Vector3.up, hit.normal) * Mathf.Deg2Rad); Debug.DrawRay(new Vector3(player.bounds.center.x, (player.bounds.center.y - player.bounds.extents.y + 0.1f), player.bounds.center.z + player.bounds.extents.z), Vector3.forward, Color.cyan, 0.3f); //calculate requiered vertical velocity for current slope; if (ang < Mathf.PI / 4) { isGround = true; float boostUp = (Mathf.Tan(ang) * (WorldMoverref.GetCurrentSpeed() * Time.deltaTime)); player.Move(Vector3.up * boostUp); } } } if ((player.collisionFlags & CollisionFlags.Above) != 0) { Debug.Log("Hit Head"); } TouchPlot.SwipeDirection swipe = plot.GetSwipeDirection(25f); bool pressingLeft = Input.GetKeyDown(KeyCode.LeftArrow) || swipe == TouchPlot.SwipeDirection.LEFT, pressingRight = Input.GetKeyDown(KeyCode.RightArrow) || swipe == TouchPlot.SwipeDirection.RIGHT, pressingUp = Input.GetKeyDown(KeyCode.Space) || swipe == TouchPlot.SwipeDirection.UP, pressingDown = Input.GetKeyDown(KeyCode.DownArrow) || swipe == TouchPlot.SwipeDirection.DOWN; // Desiered Lane if (pressingLeft) { MoveLane(false); } if (pressingRight) { MoveLane(true); } //the direction of Desiered Lane Vector3 towards = Vector3.zero; if (desiredLane == -1) { towards += Vector3.left * LANE_DISTANCE; } else if (desiredLane == 1) { towards += Vector3.right * LANE_DISTANCE; } // Current Lane if (gameObject.transform.position.x > -0.1f * LANE_DISTANCE || gameObject.transform.position.x < 0.1f * LANE_DISTANCE) { currentLane = 0; } if (gameObject.transform.position.x < -0.9f * LANE_DISTANCE) { currentLane = -1; } if (gameObject.transform.position.x > 0.9f * LANE_DISTANCE) { currentLane = 1; } //actual vector that will perform move operation Vector3 moveVector = Vector3.zero; if (Mathf.Abs((towards - gameObject.transform.position).x) < closeEnoughToLane) //when close enough teleport, stops from overshooting! { gameObject.transform.position = new Vector3(currentLane * LANE_DISTANCE, gameObject.transform.position.y, gameObject.transform.position.z); } if (isGround) { verticalVelocity = -0.1f; isJumping = false; anim.ResetTrigger("Jump"); if (pressingUp) { verticalVelocity = jumpforce; isJumping = true; anim.SetTrigger("Jump"); } else if (pressingDown || slideFromAir) { verticalVelocity = 0.0f; slideFromAir = false; if (!isSliding) { StartSliding(); Invoke("StopSliding", 1.0f); } } } else { verticalVelocity -= gravity * Time.deltaTime; if (pressingDown) { verticalVelocity = -2 * jumpforce; slideFromAir = true; anim.ResetTrigger("Jump"); } } moveVector.x = (towards - gameObject.transform.position).normalized.x * speed; moveVector.y = verticalVelocity; player.Move(Vector3.right * moveVector.x * Time.deltaTime); if ((player.collisionFlags & CollisionFlags.Sides) != 0) { desiredLane = currentLane; } gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, zedOrigin); player.Move(Vector3.up * moveVector.y * Time.deltaTime); Vector3 dir = transform.rotation.eulerAngles; if (dir != Vector3.zero) { dir.y = 0; dir.z = WorldMoverref.GetCurrentSpeed(); Vector3.Angle(Vector3.forward, dir); transform.forward = Vector3.Lerp(transform.forward, dir, 0.05f); } }