private IEnumerator DoJumpTimer(CaveGuyModel model)
        {
            model.Jumping = true;
            yield return(new WaitForSeconds(0.3f)); // 0.3 second max jump duration

            model.Jumping = false;
        }
 public void StopJumping(CaveGuyModel model)
 {
     StopCoroutine("ReduceJumpForce");
     StopCoroutine("DoJumpTimer");
     model.CurrentJumpForce = 0f;
     AddForceToJump(model); // Set the velocity back to zero
     model.Jumping = false;
 }
 public void Jump(CaveGuyModel model)
 {
     model.Jumping  = true;
     model.Grounded = false;
     model.CaveGuyRigidbody.velocity = Vector2.zero;
     StartCoroutine("ReduceJumpForce", model);
     StartCoroutine("DoJumpTimer", model);
 }
Beispiel #4
0
        public void SetYPosition(CaveGuyModel model, Vector2 relativeTo)
        {
            NewY = relativeTo.y + Offset.y;

            if (UnfollowYCouroutine != null)
            {
                StopCoroutine(UnfollowYCouroutine);
            }

            UnfollowYCouroutine = StartCoroutine(UnfollowYAfterReset());
        }
        private IEnumerator ReduceJumpForce(CaveGuyModel model)
        {
            model.CurrentJumpForce = model.MaxJumpForce;

            while (model.CurrentJumpForce > 0f)                                                                     // reduce the jump force over time
            {
                var reduction = model.CurrentJumpForce / 10f;                                                       // exponential decay with 2f
                model.CurrentJumpForce = Mathf.Clamp(model.CurrentJumpForce - reduction - 0.05f, 0f, int.MaxValue); // Subtract 0.05f to always reach zero eventually
                yield return(new WaitForFixedUpdate());
            }
        }
Beispiel #6
0
 public void ActivateDeadzoneX(CaveGuyModel model)
 {
     FollowTargetX = false;
     if ((int)Mathf.Sign(model.CaveGuyTransform.localScale.x) == 1)
     {
         LastCaveGuyXPositionLeft = model.CaveGuyTransform.position.x;
     }
     else
     {
         LastCaveGuyXPositionRight = model.CaveGuyTransform.position.x;
     }
 }
 public bool OnGround(CaveGuyModel model)
 {
     foreach (var ground in model.GroundPoints)
     {
         var colliders = Physics2D.OverlapCircleAll(ground.position, 0.05f, model.GroundLayers);
         if (colliders.FirstOrDefault(c => c.gameObject != gameObject) != null)
         {
             return(true);
         }
     }
     return(false);
 }
Beispiel #8
0
        public void FollowTarget(CaveGuyModel model)
        {
            Cam.orthographicSize = (Screen.height / 100f) / 4f;

            var x       = CameraTransform.position.x;
            var y       = CameraTransform.position.y;
            var offsetX = (Offset.x * Mathf.Sign(model.CaveGuyTransform.localScale.x)); // scale determines direction, and creates an implicit deadzone based on it's sign

            var caveGuyViewportPosition = Camera.main.WorldToViewportPoint(model.CaveGuyTransform.position);

            var newX = Mathf.MoveTowards(CameraTransform.position.x, model.CaveGuyTransform.position.x + offsetX, SmoothTimeX);

            if (!FollowTargetX)
            {
                if (caveGuyViewportPosition.x <= 0.25f || model.CaveGuyTransform.position.x < LastCaveGuyXPositionLeft) // Don't do equals otherwise it'll snap every direction change
                {
                    FollowTargetX = true;
                }
                else if (caveGuyViewportPosition.x >= 0.75f || model.CaveGuyTransform.position.x > LastCaveGuyXPositionRight) // Same as above
                {
                    FollowTargetX = true;
                }
            }

            if (FollowTargetX) // apply the new x transformation
            {
                x = newX;
            }

            if (FollowTargetY) // apply the new y transformation
            {
                y = Mathf.MoveTowards(CameraTransform.position.y, NewY, SmoothTimeY);
            }


            CameraTransform.position = new Vector3(x, y, CameraTransform.position.z); // finally, update the cameras position regardless
        }
        public void Move(CaveGuyModel model, float xAxis)
        {
            var velocity = model.CaveGuyRigidbody.velocity;

            model.CaveGuyRigidbody.velocity = new Vector2(model.Speed * xAxis, velocity.y);
        }
        public void AddForceToJump(CaveGuyModel model)
        {
            var velocity = model.CaveGuyRigidbody.velocity;

            model.CaveGuyRigidbody.velocity = new Vector2(velocity.x, model.CurrentJumpForce); // Set velocity directly for more control
        }