Example #1
0
    void Update()
    {
        // Move left or right
        if (Input.GetAxisRaw("CrabHorizontal") != 0)
        {
            // Set direction depending on left or right
            xDirection = (int)Mathf.Sign(Input.GetAxisRaw("CrabHorizontal"));
            // Directional vector
            Vector2 dir        = transform.TransformDirection(Vector2.right) * xDirection;
            float   rayOriginX = transform.position.x + (width / 2) * xDirection;
            // Raycast to from corners to left or right, depending on the value of xDirection
            RaycastHit2D hitInfoTop = Physics2D.Raycast(new Vector2(rayOriginX, transform.position.y + (height / 2) - rayOriginOffset),
                                                        dir, xSpeed * Time.deltaTime, collisionMask);
            RaycastHit2D hitInfoBottom = Physics2D.Raycast(new Vector2(rayOriginX, transform.position.y - (height / 2) + rayOriginOffset),
                                                           dir, xSpeed * Time.deltaTime, collisionMask);
            float moveDistance = 0;
            if (hitInfoBottom)
            {
                moveDistance = hitInfoBottom.distance;
            }
            else if (hitInfoTop)
            {
                moveDistance = hitInfoTop.distance;
            }
            else
            {
                moveDistance = xSpeed * Time.deltaTime;
            }
            // Move
            transform.Translate(xDirection * moveDistance, 0, 0);
        }
        // Jump
        if (Input.GetButton("CrabJump"))
        {
            if (!isJumping && OnGround())
            {
                isJumping = true;
                jumpTimer = maxJumpDuration;
            }
        }
        // If the player releases the jump button, stop the jump
        if (Input.GetButtonUp("CrabJump"))
        {
            if (jumpTimer <= minJumpDuration)
            {
                isJumping = false;
                jumpTimer = 0;
            }
            else
            {
                jumpTimer = minJumpDuration;
            }
        }

        // Check if jumping - if not, fall if not standing on ground
        if (!isJumping)
        {
            Vector2 down       = transform.TransformDirection(Vector2.down);
            float   rayOriginY = transform.position.y - (height / 2);
            // Raycast down from the bottom corners of the sprite
            RaycastHit2D hitInfoLeft = Physics2D.Raycast(new Vector2(transform.position.x - (width / 2) + rayOriginOffset, rayOriginY),
                                                         down, ySpeed * Time.deltaTime, collisionMask);
            RaycastHit2D hitInfoRight = Physics2D.Raycast(new Vector2(transform.position.x + (width / 2) - rayOriginOffset, rayOriginY),
                                                          down, ySpeed * Time.deltaTime, collisionMask);
            float moveDistance = 0;
            if (hitInfoLeft)
            {
                moveDistance = -1 * hitInfoLeft.distance;
            }
            else if (hitInfoRight)
            {
                moveDistance = -1 * hitInfoRight.distance;
            }
            else
            {
                moveDistance = -1 * ySpeed * Time.deltaTime;
            }
            transform.Translate(0, moveDistance, 0);
        }
        else
        {
            Vector2 up         = transform.TransformDirection(Vector2.up);
            float   rayOriginY = transform.position.y + (height / 2);
            // Raycast up from the top corners of the sprite
            RaycastHit2D hitInfoLeft = Physics2D.Raycast(new Vector2(transform.position.x - (width / 2) + rayOriginOffset, rayOriginY),
                                                         up, ySpeed * Time.deltaTime, collisionMask);
            RaycastHit2D hitInfoRight = Physics2D.Raycast(new Vector2(transform.position.x + (width / 2) - rayOriginOffset, rayOriginY),
                                                          up, ySpeed * Time.deltaTime, collisionMask);
            float moveDistance = 0;
            if (hitInfoLeft)
            {
                moveDistance = hitInfoLeft.distance;
                jumpTimer    = 0;
            }
            else if (hitInfoRight)
            {
                moveDistance = hitInfoRight.distance;
                jumpTimer    = 0;
            }
            else
            {
                moveDistance = ySpeed * Time.deltaTime;
            }
            transform.Translate(0, moveDistance, 0);

            // Count down jump timer
            jumpTimer--;
            // Stop jumping?
            if (jumpTimer <= 0)
            {
                isJumping = false;
            }
        }
        invincibility.Update();
    }
 // Update is called once per frame
 void Update()
 {
     if (!inDiving)
     {
         if (Input.GetAxisRaw("Fire1") > 0)
         {
             keepDiving = true;
             if (!inDiving)
             {
                 StartCoroutine(DivingRoutine(diveDepth, new Vector3((float)horizontalSpeed, (float)verticalSpeed, 0)));
                 if (animator != null)
                 {
                     animator.SetBool("isDiving", true);
                 }
                 inDiving = true;
             }
         }
         if (Input.GetAxisRaw("Fire2") > 0 && (shotCoolDown == 0))
         {
             if (pewSound != null)
             {
                 pewSound.Play();
             }
             GameObject go  = Instantiate(duckShotResource) as GameObject;
             Vector3    tmp = collider.bounds.center;
             tmp.x = collider.bounds.max.x + go.GetComponent <Collider2D>().bounds.size.x;
             go.transform.position = tmp;
             DuckShotController dsc = go.GetComponent <DuckShotController>();
             dsc.enabled           = true;
             dsc.movementDirection = transform.right;
             shotCoolDown          = shotDisableFrames;
         }
     }
     else
     {
         if (Input.GetAxisRaw("Fire1") <= 0)
         {
             if (animator != null)
             {
                 animator.SetBool("isDiving", false);
             }
             keepDiving = false;
         }
     }
     if (Input.GetAxisRaw("Fire3") > 0)
     {
         if (!crabCaught && crabButtonTimer == 0)
         {
             float   distance;
             Vector3 rayDirection, rayJump, startPos;
             CalculateNecesaryValues(15, RayDirectionSearch.DOWN, collider.bounds, collider.bounds.size.y, 0, transform, false, out distance, out rayDirection, out rayJump, out startPos);
             Vector3      moveVector;
             RaycastHit2D tmp;
             RaycastHit2D rh = DoRaycast(15, RayDirectionSearch.DOWN, distance, rayDirection, rayJump, startPos, out moveVector, out tmp, "Crab");
             if (rh.collider != null)
             {
                 rh.collider.transform.parent   = transform;
                 rh.collider.transform.position = crabPos.position;
                 crabTransform = rh.collider.transform;
                 CrabController cc = crabTransform.GetComponent <CrabController>();
                 cc.enabled = false;
                 BoxCollider2D circlC = crabTransform.GetComponent <BoxCollider2D>();
                 circlC.enabled = false;
                 Debug.Log("Doing the GRAB FFS");
                 crabCaught      = true;
                 crabCaughtTimer = crabCaughtMaxHoldTime;
             }
             crabButtonTimer = crabButtonCoolDown;
         }
         else if (crabButtonTimer == 0)
         {
             ReleasCrab();
         }
     }
     if (crabCaught && crabCaughtTimer == 0)
     {
         ReleasCrab();
     }
     if (shotCoolDown > 0)
     {
         shotCoolDown--;
     }
     if (crabButtonTimer > 0)
     {
         crabButtonTimer--;
     }
     if (crabCaughtTimer > 0)
     {
         crabCaughtTimer--;
     }
     // Update invincibility counter n' stuff
     invincibility.Update();
 }