// Update is called once per frame
    void Update()
    {
        _moveDirection.x  = Input.GetAxis("Horizontal");
        _moveDirection.x *= moveSpeed;

        if (isGrounded)
        {
            _moveDirection.y = 0;

            if (Input.GetButtonDown("Jump"))
            {
                _moveDirection.y = jumpSpeed;
                isJumping        = true;
            }
        }
        else
        {
        }
        _moveDirection.y -= gravity * Time.deltaTime;

        _characterController.move(_moveDirection * Time.deltaTime);
        flags = _characterController.collisionState;

        isGrounded = flags.below;
        if (flags.above)
        {
            _moveDirection.y = gravity * Time.deltaTime;
        }
    }
Exemple #2
0
    protected virtual void Update()
    {
        // Gravity and Movement
        // if (!stopGravity)
        // {
        //     moveDirection.y -= gravity * Time.deltaTime;
        //     // moving left or right (or jump or fall)
        //     cc2d.move(moveDirection * Time.deltaTime);
        // }


        //if (!stopGravity || isClimbing) // order??????
        if (!isClimbing) // order??????
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }
        cc2d.move(moveDirection * Time.deltaTime);

        // report what is around us
        flags = cc2d.collisionState;

        /// Check if we are on the ground
        isGrounded = flags.below;
        //isRight = flags.right;
        //isLeft = flags.left;

        /// Check if we hit our head
        // if (flags.above)
        // {
        //     moveDirection.y -= gravity * Time.deltaTime;
        //     moveDirection.x = 0;
        // }
    }
Exemple #3
0
    protected virtual void Update()
    {
        if (!stopGravity)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        cc2d.move(moveDirection * Time.deltaTime);

        // report what is around us
        flags = cc2d.collisionState;

        //// Check if we are on the ground
        isGrounded = flags.below;
        //isAbove = flags.above;
        isRight = flags.right;
        isLeft  = flags.left;
    }
Exemple #4
0
 void Start()
 {
     character      = GetComponent <CharacterController2D>();
     collisionState = character.collisionState;
 }
Exemple #5
0
    void Update()
    {
        if (!isHurt)
        {
            moveDirection.x  = Input.GetAxis("Horizontal"); // recupera valor dos controles
            moveDirection.x *= walkSpeed;
        }
        else
        {
            moveDirection.x *= 0.95f;
        }


        // Conforme direção do personagem girar ele no eixo Y
        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            isFacingRight         = false;
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            isFacingRight         = true;
        } // se direção em x == 0 mantenha como está a rotação



        timer      += Time.deltaTime;
        timer_hurt += Time.deltaTime;
        if (timer > wait && Input.GetButton("Fire1") && !isDead)
        {
            timer = 0;

            audioSource.PlayOneShot(audio_attack);
            audioSource.PlayOneShot(audio_fire);


            StartCoroutine(do_attack());
        }



        if (isGrounded && !isDead)
        {                           // caso esteja no chão
            moveDirection.y = 0.0f; // se no chão nem subir nem descer

            isJumping    = false;
            doubleJumped = false; // se voltou ao chão pode faz pulo duplo


            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
                isJumping       = true;

                audioSource.PlayOneShot(audio_jump);
            }
        }
        else
        {
            // caso esteja pulando
            if (Input.GetButtonUp("Jump") && moveDirection.y > 0) // Soltando botão diminui pulo
            {
                moveDirection.y *= 0.5f;
            }

            if (Input.GetButtonDown("Jump") && !doubleJumped) // Segundo clique faz pulo duplo
            {
                moveDirection.y = doubleJumpSpeed;
                doubleJumped    = true;
                audioSource.PlayOneShot(audio_jump);
            }
        }

        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 4f, mask);

        if (hit.collider != null && isGrounded)
        {
            transform.SetParent(hit.transform);
            if (Input.GetAxis("Vertical") < 0)
            {
                moveDirection.y = -jumpSpeed;
                StartCoroutine(PassPlatform(hit.transform.gameObject));
            }
        }
        else
        {
            transform.SetParent(null);
        }

        if (moveDirection.y < 0)
        {
            isFalling = true;
        }
        else
        {
            isFalling = false;
        }


        moveDirection.y -= gravity * Time.deltaTime;              // aplica a gravidade
        characterController.move(moveDirection * Time.deltaTime); // move personagem

        flags      = characterController.collisionState;          // recupera flags
        isGrounded = flags.below;                                 // define flag de chão


        if (Input.GetAxis("Vertical") < 0 && moveDirection.x == 0 && !isDead)
        {
            if (!isDucking)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, 2 * colliderSizeY / 3);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY - colliderSizeY / 6);
                characterController.recalculateDistanceBetweenRays();
            }
            isDucking = true;
        }
        else
        {
            if (isDucking)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, colliderSizeY);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY);
                characterController.recalculateDistanceBetweenRays();
                isDucking = false;
            }
        }


        animator.SetFloat("movementX", Mathf.Abs(moveDirection.x / walkSpeed)); // +Normalizado
        animator.SetFloat("movementY", moveDirection.y);
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isJumping", isJumping);
        animator.SetBool("isDucking", isDucking);
        animator.SetBool("isFalling", isFalling);
        animator.SetBool("isAttacking", isAttacking);
        animator.SetBool("isDoubleJump", doubleJumped);
    }
    // Update is called once per frame
    void Update()
    {
        //sets the player movement for X
        if (wallJumped == false)
        {
            moveDirection.x = Input.GetAxis("Horizontal");
            if (isCreeping)
            {
                moveDirection.x *= creepSpeed;
            }
            else
            {
                moveDirection.x *= walkSpeed;
            }
        }
        #region Ground angle check
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector3.up, 2f, layerMask);
        if (hit)
        {
            slopeAngle    = Vector2.Angle(hit.normal, Vector2.up);
            slopeGradient = hit.normal;
            if (slopeAngle > characterController.slopeLimit)
            {
                isSlopeSliding = true;
            }
            else
            {
                isSlopeSliding = false;
            }
        }
        #endregion
        #region Player on ground
        //player is on the ground
        if (isGrounded)
        {
            moveDirection.y   = 0;
            isJumping         = false;
            doubleJumped      = false;
            isStomping        = false;
            currentGlideTimer = glideTimer;
            if (moveDirection.x < 0)
            {
                transform.eulerAngles = new Vector3(0, 180, 0);
                isFacingRight         = false;
            }
            else if (moveDirection.x > 0)
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                isFacingRight         = true;
            }

            if (isSlopeSliding)
            {
                moveDirection = new Vector3(slopeGradient.x * slopeSlideSpeed, slopeGradient.y * slopeSlideSpeed, 0f);
            }
            if (Input.GetButtonDown("Jump"))
            {
                if (canPowerJump && isDucking)
                {
                    moveDirection.y = jumpSpeed + powerJumpSpeed;
                    StartCoroutine("PowerJumpWaiter");
                }
                else
                {
                    moveDirection.y = jumpSpeed;
                    isJumping       = true;
                }
                ableToWallRun = true;
            }
        }
        #endregion
        #region Player in the air
        else
        {
            if (Input.GetButtonUp("Jump"))
            {
                if (moveDirection.y > 0)
                {
                    moveDirection.y = moveDirection.y * 0.5f;
                }
            }
            if (Input.GetButtonDown("Jump"))
            {
                //double jump
                if (canDoubleJump)
                {
                    if (!doubleJumped)
                    {
                        moveDirection.y = doubleJumpSpeed;
                        doubleJumped    = true;
                    }
                }
            }
        }
        #endregion
        #region Gravity Calculations
        //Controls gliding for jetpack
        if (canGlide == true && Input.GetAxis("Vertical") > 0.5f && characterController.velocity.y < 0.2f)
        {
            if (currentGlideTimer > 0)
            {
                isGliding = true;
                if (startGlide)
                {
                    moveDirection.y = 0;
                    startGlide      = false;
                }
                moveDirection.y   -= glideAmount * Time.deltaTime;
                currentGlideTimer -= Time.deltaTime;
            }
            else
            {
                isGliding        = false;
                moveDirection.y -= gravity * Time.deltaTime;
            }
        }
        else if (canStomp && isDucking && !isPowerJumping)
        {
            moveDirection.y -= gravity * Time.deltaTime + stompSpeed;
            isStomping       = true;
        }
        else
        {
            isGliding        = false;
            startGlide       = true;
            moveDirection.y -= gravity * Time.deltaTime;
        }
        #endregion
        //update the CharacterController
        characterController.move(moveDirection * Time.deltaTime);
        flags = characterController.collisionState;

        #region Ducking and creeping
        frontTopCorner = new Vector3(transform.position.x + boxCollider.size.x / 2, transform.position.y + boxCollider.size.y / 2, 0);
        backTopCorner  = new Vector3(transform.position.x - boxCollider.size.x / 2, transform.position.y + boxCollider.size.y / 2, 0);
        RaycastHit2D hitFrontCeiling = Physics2D.Raycast(frontTopCorner, Vector2.up, 2f, layerMask);
        RaycastHit2D hitBackCeiling  = Physics2D.Raycast(backTopCorner, Vector2.up, 2f, layerMask);
        if (Input.GetAxis("Vertical") < 0 && moveDirection.x == 0)
        {
            if (!isDucking && !isCreeping)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, boxCollider.size.y / 2);
                transform.position = new Vector3(transform.position.x, transform.position.y - (originalBoxCollider.y / 4), 0);
                characterController.recalculateDistanceBetweenRays();
            }
            isCreeping = false;
            isDucking  = true;
        }
        else if (Input.GetAxis("Vertical") < 0 && (moveDirection.x < 0 || moveDirection.x > 0))
        {
            if (!isDucking && !isCreeping)
            {
                boxCollider.size = new Vector2(boxCollider.size.x, boxCollider.size.y / 2);
                characterController.recalculateDistanceBetweenRays();
            }
            isCreeping = true;
            isDucking  = false;
        }
        else
        {
            if ((!hitFrontCeiling.collider && !hitBackCeiling) && (isDucking || isCreeping))
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, originalBoxCollider.y);
                transform.position = new Vector3(transform.position.x, transform.position.y + (originalBoxCollider.y / 4), 0);
                characterController.recalculateDistanceBetweenRays();
                isCreeping = false;
                isDucking  = false;
            }
        }
        #endregion
        if (flags.above)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }
        #region wall running and jumping
        if (flags.left || flags.right)
        {
            if (canWallRun)
            {
                if (Input.GetAxis("Vertical") > 0 && ableToWallRun == true && isGrounded == false)
                {
                    moveDirection.y = jumpSpeed / wallRunAmount;
                    StartCoroutine(WallRunWaiter());

                    if (flags.left)
                    {
                        transform.eulerAngles = new Vector3(0, 180, 0);
                    }
                    else if (flags.right)
                    {
                        transform.eulerAngles = new Vector3(0, 0, 0);
                    }
                }
            }

            if (canWallJump)
            {
                if (Input.GetButtonDown("Jump") || wallJumped == false && isGrounded == false)
                {
                    if (moveDirection.x < 0)
                    {
                        moveDirection.x       = jumpSpeed * wallJumpXAmount;
                        moveDirection.y       = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 180, 0);
                        lastJumpWasLeft       = false;
                    }
                    else if (moveDirection.x > 0)
                    {
                        moveDirection.x       = jumpSpeed * wallJumpXAmount;
                        moveDirection.y       = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        lastJumpWasLeft       = true;
                    }
                    StartCoroutine(WallJumpWaiter());
                    if (canRunAfterWallJump)
                    {
                        doubleJumped = false;
                    }
                    else
                    {
                        ableToWallRun = false;
                    }
                }
            }
        }
        else
        {
            if (canRunAfterWallJump)
            {
                StopCoroutine(WallRunWaiter());
                ableToWallRun = true;
                isWallRunning = false;
            }
        }
        #endregion
        isGrounded = flags.below;
        UpdateAnimator();
    }
    // Update is called once per frame
    void Update()
    {
        if (wallJumped == false)
        {
            _moveDirection.x  = Input.GetAxis("Horizontal");
            _moveDirection.x *= walkSpeed;
        }


        if (isGrounded) //player is grounded
        {
            _moveDirection.y = 0;
            isJumping        = false;
            doubleJumped     = false;


            if (_moveDirection.x < 0)
            {
                transform.eulerAngles = new Vector3(0, 180, 0);
                isFactingRight        = false;
            }
            else if (_moveDirection.x > 0)
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                isFactingRight        = true;
            }

            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                _moveDirection.y = jumpSpeed;
                isJumping        = true;

                isWallRunning = true;
            }
        }
        else //player is in the air
        {
            if (Input.GetKeyUp(KeyCode.UpArrow))
            {
                if (_moveDirection.y > 0)
                {
                    _moveDirection.y = _moveDirection.y * 0.5f;
                }
            }
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                if (canDoubleJump)
                {
                    if (!doubleJumped)
                    {
                        _moveDirection.y = doublejumpSpeed;
                        doubleJumped     = true;
                    }
                }
            }
        }
        _moveDirection.y -= gravity * Time.deltaTime;

        _characterController.move(_moveDirection * Time.deltaTime);

        flags = _characterController.collisionState;

        isGrounded = flags.below;

        if (flags.above)
        {
            _moveDirection.y -= gravity * Time.deltaTime;
        }
        if (flags.left || flags.right)
        {
            if (canWallRun)
            {
                if (Input.GetAxis("Vertical") > 0 && isWallRunning == true)
                {
                    _moveDirection.y = jumpSpeed / WallRunAmount;
                    StartCoroutine(WallRunWaiter());
                }
            }
            if (canWallJupm)
            {
                if (Input.GetKeyDown(KeyCode.UpArrow) || wallJumped == false && isGrounded == false)
                {
                    if (_moveDirection.x < 0)
                    {
                        _moveDirection.x      = jumpSpeed * wallXJumpAmount;
                        _moveDirection.y      = jumpSpeed * wallYJumpAmount;
                        transform.eulerAngles = new Vector3(0, 180, 0);
                        _lastJumpWasLeft      = false;
                    }
                    else if (_moveDirection.x > 0)
                    {
                        _moveDirection.x      = -jumpSpeed * wallXJumpAmount;
                        _moveDirection.y      = jumpSpeed * wallYJumpAmount;
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        _lastJumpWasLeft      = true;
                    }
                    StartCoroutine(WallJumpWaiter());
                    if (canJumpAfterWallJump)
                    {
                        doubleJumped = false;
                    }
                }
            }
        }

        /*     else
         *   {
         *       if (canRunAfterWallJump)
         *       {
         *           StopCoroutine(WallRunWaiter());
         *           isWallRunning = true ;
         *       }
         *   }*/
    }
Exemple #8
0
    void Update()
    {
        if (!wallJumped)
        {
            _moveDirection.x  = Input.GetAxis("Horizontal");
            _moveDirection.x *= walkSpeed;
        }

        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector3.up, 1f, layerMask);

        if (hit)
        {
            _slopeAngle    = Vector2.Angle(hit.normal, Vector2.up); //normal vector vs upward vector
            _slopeGradient = hit.normal;

            if (_slopeAngle > _characterController.slopeLimit)
            {
                isSlopeSliding = true;
            }
            else
            {
                isSlopeSliding = false;
            }
        }

        if (isGrounded)  //while player is on the ground
        {
            _currentGlideTimer = glideTimer;
            _moveDirection.y   = 0;
            isJumping          = false;
            doubleJumped       = false;
            if (_moveDirection.x < 0)                           //players moving left
            {
                transform.eulerAngles = new Vector3(0, 180, 0); //rotate players on the y axis
                facingRight           = false;
                _anim.SetInteger("State", 1);
            }
            else if (_moveDirection.x > 0)
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                facingRight           = true;
                _anim.SetInteger("State", 1);
            }
            else
            {
                _anim.SetInteger("State", 0);
            }

            if (isSlopeSliding)
            {
                _moveDirection = new Vector3(_slopeGradient.x * slopeSlideSpeed, -_slopeGradient.y * slopeSlideSpeed, 0f);
            }

            if (Input.GetButtonDown("Jump"))
            {
                _moveDirection.y = jumpSpeed;
                isJumping        = true;
                isWallRunning    = true;
                _anim.SetInteger("State", 2);
            }
        }
        else  //while player is in the air
        {
            if (Input.GetButtonUp("Jump"))
            {
                if (_moveDirection.y > 0)
                {
                    _moveDirection.y = _moveDirection.y * 0.5f;
                    _anim.SetInteger("State", 3);
                }
            }

            if (Input.GetButtonDown("Jump"))
            {
                if (canDoubleJump)
                {
                    if (!doubleJumped)
                    {
                        _anim.SetInteger("State", 2);
                        _moveDirection.y = doubleJumpSpeed;
                        doubleJumped     = true;
                    }
                }
            }
        }

        //Handle Gliding
        if (canGlide && Input.GetAxis("Vertical") > 0.5f && _characterController.velocity.y < 0.2f && !isGrounded)
        {
            if (_currentGlideTimer > 0)
            {
                isGliding = true;
                if (_startGlide)
                {
                    _moveDirection.y = 0;
                    _startGlide      = false;
                }
                _moveDirection.y   -= glideAmount * Time.deltaTime;
                _currentGlideTimer -= Time.deltaTime;
                _anim.SetInteger("State", 3);
            }
            else
            {
                isGliding         = false;
                _moveDirection.y -= gravity * Time.deltaTime;
                _anim.SetInteger("State", 3);
            }
        }
        else
        {
            isGliding         = false;
            _startGlide       = true;
            _moveDirection.y -= gravity * Time.deltaTime;
        }

        _characterController.move(_moveDirection * Time.deltaTime);
        // to make sure that it is frame rate independent;

        flags = _characterController.collisionState;
        //get collisions informations

        isGrounded = flags.below;

        if (flags.above)
        {
            _moveDirection.y -= gravity * Time.deltaTime;
        }

        if (flags.left || flags.right)
        {
            if (canWallRun)
            {
                if (Input.GetAxis("Vertical") > 0 && isWallRunning)
                {
                    _moveDirection.y = jumpSpeed / wallRunAmount;
                    StartCoroutine(wallRunWaiter());
                }
            }

            if (canWallJump)
            {
                if (Input.GetButtonDown("Jump") && !wallJumped && !isGrounded)
                {
                    //perform wall jump
                    if (_moveDirection.x < 0)
                    {
                        //moving to the left
                        _moveDirection.x      = jumpSpeed * wallJumpXAmount;
                        _moveDirection.y      = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        _lastJumpWasLeft      = false;
                    }
                    else if (_moveDirection.x > 0)
                    {
                        //moving to the right
                        _moveDirection.x      = -jumpSpeed * wallJumpXAmount;
                        _moveDirection.y      = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 180, 0);
                        _lastJumpWasLeft      = true;
                    }

                    StartCoroutine(wallJumpWaiter());
                }
            }
        }
        else
        {
            if (canRunAfterWallJump)
            {
                StopCoroutine(wallRunWaiter());
                isWallRunning = true;
            }
        }
    }
Exemple #9
0
    void Update()
    {
        if (isGrounded)
        {
            //STOP
            if (groundMovementState.Equals(GroundMovementState.Stop))
            {
                _moveDirection = Vector3.zero;
                if (isGrounded && !jumpAndWait)
                {
                    StartCoroutine("MoveForwardFromStop");
                }
                //rb.velocity = new Vector2(0, 0);
                //rb.angularVelocity = 0f;
            }
            //MOVE FORWARD
            else if (groundMovementState.Equals(GroundMovementState.MoveForward))
            {
                if (_isFacingLeft)
                {
                    _moveDirection.x = -moveSpeed;
                }
                else
                {
                    _moveDirection.x = moveSpeed;
                }
            }
            //JUMP
            else if (groundMovementState.Equals(GroundMovementState.Jump))
            {
                jumpAndWait = true;
                if (jumpAndWait)
                {
                    StartCoroutine(JumpAndWait());
                }
                _moveDirection.y = jumpSpeed;

                if (jumpForward && _isFacingLeft)
                {
                    _moveDirection.x = -moveSpeed;
                }
                else if (jumpForward && !_isFacingLeft)
                {
                    _moveDirection.x = moveSpeed;
                }
            }
            ////PATROL
            //else if (groundMovementState.Equals(GroundMovementState.Patrol))
            //{
            //    if (!_currentTarget)
            //        _currentTarget = wayPoints[_wayPointCounter];

            //    Vector3 difference = _currentTarget.position - transform.position;
            //    float distanceX = Mathf.Abs(difference.x);

            //    if(distanceX > 0.1f)
            //    {
            //        //current target is to the right of the enemy
            //        if(difference.x > 0f)
            //        {
            //            _moveDirection.x = moveSpeed;
            //            transform.eulerAngles = new Vector3(0, 180, 0);
            //        }
            //       else if (difference.x < 0f)
            //        {
            //            _moveDirection.x = -moveSpeed;
            //            transform.eulerAngles = new Vector3(0, 0, 0);
            //        }
            //    }
            //    else
            //    {
            //        StartCoroutine("ArriveAtWaypoint");
            //    }
            //}
            //else if (groundMovementState.Equals(GroundMovementState.Dash))
            //{

            //}
        }
        // if isGrounded Ends HERE


        _moveDirection.y -= gravity * Time.deltaTime;
        _characterController.move(_moveDirection * Time.deltaTime);
        //flags, will tell you if your controler has collided (asdw)
        _flags = _characterController.collisionState;
        //detect if the E is on the ground, if it is then isGrounded = true
        isGrounded = _flags.below;

        if (autoTurn)
        {
            if (_flags.left && _isFacingLeft)
            {
                Turn();
            }
            else if (_flags.right && !_isFacingLeft)
            {
                Turn();
            }
        }
    }
    void Update()
    {
        animator.SetFloat("movementX", Mathf.Abs(moveDirection.x / walkSpeed)); // +Normalizado
        animator.SetFloat("movementY", moveDirection.y);
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isJumping", isJumping);
        animator.SetBool("isFalling", isFalling);
        animator.SetBool("isFlying", isFlying);
        animator.SetBool("isShooting", isShooting);
        animator.SetBool("isClimbing", isClimbing);

        if (GameObject.Find("Player").transform.position.x > 12.5 && GameObject.Find("Player").transform.position.y < 3.3)
        {
            SoundMain.SetActive(false);
            SoundCave.SetActive(true);
        }
        else
        {
            SoundMain.SetActive(true);
            SoundCave.SetActive(false);
        }

        moveDirection.x  = Input.GetAxis("Horizontal"); // recupera valor dos controles
        moveDirection.x *= walkSpeed;


        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 4f, mask);

        if (hit.collider != null && isGrounded)
        {
            transform.SetParent(hit.transform);
            if (Input.GetAxis("Vertical") < 0 && Input.GetButtonDown("Jump"))
            {
                moveDirection.y = -jumpSpeed;
                StartCoroutine(PassPlatform(hit.transform.gameObject));
            }
        }
        else
        {
            transform.SetParent(null);
        }

        if (isGrounded == true)
        {
            isFalling = false;
        }
        else if (moveDirection.y < 1)
        {
            isFalling = true;
            isFlying  = false;
        }
        else
        {
            isFalling = false;
        }

        // Conforme direção do personagem girar ele no eixo Y
        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            isFacingRight         = false;
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            isFacingRight         = true;
        } // se direção em x == 0 mantenha como está a rotação

        if (isGrounded)
        {                           // caso esteja no chão
            moveDirection.y = 0.0f; // se no chão nem subir nem descer

            isJumping    = false;
            doubleJumped = false; // se voltou ao chão pode faz pulo duplo
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
                isJumping       = true;
            }
        }
        else
        {                                                         // caso esteja pulando
            if (Input.GetButtonUp("Jump") && moveDirection.y > 0) // Soltando botão diminui pulo
            {
                moveDirection.y *= 0.5f;
            }

            if (Input.GetButtonDown("Jump") && !doubleJumped) // Segundo clique faz pulo duplo
            {
                moveDirection.y = doubleJumpSpeed;
                doubleJumped    = true;
                isFlying        = true;
            }
        }


        moveDirection.y -= gravity * Time.deltaTime;    // aplica a gravidade

        animator.speed = 1.0f;
        if (isClimbing)
        {
            if (Input.GetAxis("Vertical") > 0)
            {
                moveDirection.y = walkSpeed;
                isJumping       = true;
            }
            else if (Input.GetAxis("Vertical") < 0)
            {
                moveDirection.y = -walkSpeed;
            }
            else
            {
                if (!isGrounded)
                {
                    moveDirection.y = 0.0f;
                    animator.speed  = 0.0f;
                }
            }
        }



        characterController.move(moveDirection * Time.deltaTime); // move personagem

        flags      = characterController.collisionState;          // recupera flags
        isGrounded = flags.below;                                 // define flag de chão

        if (Input.GetKeyDown("b") && isShooting == false)
        {
            audioSource.PlayOneShot(Atirando);
            isShooting = true;
            shootBullet();
        }
    }
Exemple #11
0
    public override void Tick()
    {
        currentCoyoteTime += Time.deltaTime;

        if (currentCoyoteTime <= coyoteTime && slime.playerInput.GetDidPressJumpBuffered())
        {
            slime.velocity.y = coyoteJumpPower;
        }

        if (slime.velocity.y > slime.minJumpVelocity && slime.playerInput.GetDidReleaseJump())
        {
            slime.velocity.y = slime.minJumpVelocity;
        }

        float horizInput = slime.playerInput.GetHorizInput();
        // move
        float targetVelocityX = horizInput * slime.horizSpeed;

        bool isBoosted = Mathf.Abs(slime.velocity.x) > (slime.horizSpeed + boostedVelocitySlop);

        if (slime.lockAirborneMovementTime <= 0f)
        {
            if (isBoosted && horizInput != 0f && (Mathf.Sign(horizInput) == Mathf.Sign(slime.velocity.x)))
            {
                // don't need to slow him down!
            }
            else
            {
                slime.velocity.x = Mathf.SmoothDamp(
                    slime.velocity.x,
                    targetVelocityX,
                    ref slime.velocityXSmoothing,
                    slime.velocityXSmoothFactorAirborne);
            }
        }

        if (slime.velocity.x != 0f)
        {
            slime.FaceMovementDirection();
        }

        slime.velocity.y += slime.gravity * Time.deltaTime;
        slime.controller.Move(slime.velocity * Time.deltaTime);

        // check if hit ground
        CharacterController2D.CharacterCollisionState2D collisions = slime.controller.collisionState;
        if (collisions.below)
        {
            slime.velocity.y = 0f;
            slime.fsm.ChangeState(slime.stateGrounded, slime.stateGrounded, true);
            SpawnLandEffect();
            return;
        }

        if (slime.playerInput.GetDidPressAttack())
        {
            SpawnAirPlatform();
        }

        if (IsCollidingWithWall(horizInput))
        {
            slime.velocity.x = 0f;
            slime.velocity.y = 0f;
            bool isWallOnLeft = horizInput < 0;
            slime.fsm.ChangeState(slime.stateWallCling, slime.stateWallCling, isWallOnLeft);
            return;
        }

        curFallTime += Time.deltaTime;
        if (curFallTime > maxFallTime)
        {
            slime.Die();
        }
    }
    void Update()
    {
        if (wallJumped == false)
        {
            _moveDirection.x  = Input.GetAxis("Horizontal");
            _moveDirection.x *= walkSpeed;
        }
        //Check ground angle for slope slide
        GetGroundType();


        if (isGrounded) //Player is grounded
        {
            _moveDirection.y   = 0;
            isJumping          = false;
            doubleJumped       = false;
            isStomping         = false;
            _currentGlideTimer = glideTimer;

            if (_moveDirection.x < 0)                           //P moving towards the left
            {
                transform.eulerAngles = new Vector3(0, 180, 0); // rotating P 180 degrees towards Y axis
                isFacingRight         = false;
            }
            else if (_moveDirection.x > 0) // P -> right
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                isFacingRight         = true;
            }

            if (isSlopeSliding)
            {
                _moveDirection = new Vector3(_slopeGradient.x * slopeSlideSpeed, -_slopeGradient.y * slopeSlideSpeed, 0f);
            }

            //JUMP && PowerJump
            if (Input.GetButtonDown("Jump"))
            {
                if (canPowerJump && isDucking)
                {
                    _moveDirection.y = jumpSpeed + powerJumpSpeed;
                    StartCoroutine("PowerJumpWaiter");
                }
                else
                {
                    _moveDirection.y = jumpSpeed;
                    isJumping        = true;
                }


                isWallRunning = true;
            }
        }
        else //Player is in the air
        {
            if (Input.GetButtonUp("Jump"))
            {
                if (_moveDirection.y > 0) //P istill going up
                {
                    _moveDirection.y = _moveDirection.y * 0.5f;
                }
            }

            if (Input.GetButtonDown("Jump"))
            {
                if (canDoubleJump)
                {
                    if (!doubleJumped)
                    {
                        _moveDirection.y = doubleJumpSpeed;
                        doubleJumped     = true;
                    }
                }
            }
        }
        //GRAVITY CALCULATIONS
        if (canGlide = true && Input.GetAxis("Vertical") > 0.5f && _characterController.velocity.y < 0.2f)
        {
            if (_currentGlideTimer > 0) //We still have glide time
            {
                //Overwriting gravity calculations
                isGliding = true; //Important for animation

                if (_startGlide)
                {
                    _moveDirection.y = 0;
                    _startGlide      = false;
                }

                _moveDirection.y   -= glideAmount * Time.deltaTime;
                _currentGlideTimer -= Time.deltaTime;
            }
            else
            {
                isGliding         = false;
                _moveDirection.y -= gravity * Time.deltaTime; //Player is affected by gravity. (We stop gliding)
            }
        }
        else if (canStomp && isDucking && !isPowerJumping)
        {
            _moveDirection.y -= gravity * Time.deltaTime + stompSpeed;
            isStomping        = true;
        }
        else
        {
            isGliding         = false;
            _startGlide       = true;
            _moveDirection.y -= gravity * Time.deltaTime; //Player is affected by gravity
        }

        //PLAYER MOVEMENT
        _characterController.move(_moveDirection * Time.deltaTime); //Tells CharacterController to move player left or right
        _flags = _characterController.collisionState;               //Returns the flags:that tell us if its anything to our left us, right or above or below


        isGrounded = _flags.below;

        //DUCKING AND CREEPING

        _frontTopCorner = new Vector3(transform.position.x + _boxCollider.size.x / 2, transform.position.y + _boxCollider.size.y / 2, 0);
        _backTopCorner  = new Vector3(transform.position.x - _boxCollider.size.x / 2, transform.position.y + _boxCollider.size.y / 2, 0);
        RaycastHit2D hitFrontCeiling = Physics2D.Raycast(_frontTopCorner, Vector2.up, 2f, layerMask); //LevelGeometryMask
        RaycastHit2D hitBackCeiling  = Physics2D.Raycast(_backTopCorner, Vector2.up, 2f, layerMask);  //LevelGeometryMask

        if (Input.GetAxis("Vertical") < 0 && _moveDirection.x == 0)
        {
            if (!isDucking && !isCreeping)
            {
                _boxCollider.size  = new Vector2(_boxCollider.size.x, _originalBoxColliderSize.y / 2);
                transform.position = new Vector3(transform.position.x, transform.position.y - (_originalBoxColliderSize.y / 4), 0);
                _characterController.recalculateDistanceBetweenRays();
            }

            isDucking  = true;
            isCreeping = false;
        }
        else if (Input.GetAxis("Vertical") < 0 && (_moveDirection.x < 0 || _moveDirection.x > 0))
        {
            if (!isDucking && !isCreeping)
            {
                _boxCollider.size  = new Vector2(_boxCollider.size.x, _originalBoxColliderSize.y / 2);
                transform.position = new Vector3(transform.position.x, transform.position.y - (_originalBoxColliderSize.y / 4), 0);
                _characterController.recalculateDistanceBetweenRays();
            }

            isDucking  = false;
            isCreeping = true;
        }
        else
        {
            //Where we stand back up
            if (!hitFrontCeiling.collider && !hitBackCeiling.collider && (isDucking || isCreeping))
            {
                _boxCollider.size  = new Vector2(_boxCollider.size.x, _originalBoxColliderSize.y);
                transform.position = new Vector3(transform.position.x, transform.position.y + (_originalBoxColliderSize.y / 4), 0);
                _characterController.recalculateDistanceBetweenRays();

                isDucking  = false;
                isCreeping = false;
            }
        }


        if (_flags.above)
        {
            _moveDirection.y -= gravity * Time.deltaTime;
        }


        //WALL RUN
        if (_flags.left || _flags.right)
        {
            if (canWallRun)
            {
                if (Input.GetAxis("Vertical") > 0 && isWallRunning == true)
                {
                    _moveDirection.y = jumpSpeed / wallRunAmount;
                    StartCoroutine(WallRunWaiter());
                }
            }

            //WALL JUMP
            if (canWallJump)
            {
                if (Input.GetButtonDown("Jump") && wallJumped == false && isGrounded == false)
                {
                    if (_moveDirection.x < 0) // Left
                    {
                        _moveDirection.x      = jumpSpeed * wallJumpXAmount;
                        _moveDirection.y      = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        //_lastJumpWasLeft = false;
                        wallJumped = true;
                    }
                    else if (_moveDirection.x > 0)
                    {
                        _moveDirection.x      = -jumpSpeed * wallJumpXAmount;
                        _moveDirection.y      = jumpSpeed * wallJumpYAmount;
                        transform.eulerAngles = new Vector3(0, 180, 0);
                        //_lastJumpWasLeft = true;
                        wallJumped = true;
                    }

                    StartCoroutine(WallJumpWaiter());
                }
            }
        }
        else
        {
            if (canRunAfterWallJump)
            {
                StopCoroutine(WallRunWaiter());
                isWallRunning = true;
            }
        }
    }
Exemple #13
0
    public override void Tick()
    {
        currentCoyoteTime += Time.deltaTime;

        if (currentCoyoteTime <= coyoteTime && manfred.playerInput.GetDidPressJumpBuffered())
        {
            manfred.velocity.y = coyoteJumpPower;
        }

        if (manfred.velocity.y > manfred.minJumpVelocity && manfred.playerInput.GetDidReleaseJump())
        {
            manfred.velocity.y = manfred.minJumpVelocity;
        }

        float horizInput = manfred.playerInput.GetHorizInput();

        if (manfred.lockAirborneMovementTime <= 0f)
        {
            // move
            float targetVelocityX = horizInput * manfred.horizSpeed;
            manfred.velocity.x = Mathf.SmoothDamp(
                manfred.velocity.x,
                targetVelocityX,
                ref manfred.velocityXSmoothing,
                manfred.velocityXSmoothFactorAirborne);
        }

        if (manfred.velocity.x != 0f)
        {
            manfred.FaceMovementDirection();
        }

        manfred.velocity.y += manfred.gravity * Time.deltaTime;
        manfred.controller.Move(manfred.velocity * Time.deltaTime);

        if (manfred.playerInput.GetDidPressParry())
        {
            manfred.fsm.ChangeState(manfred.stateUseCard, manfred.stateUseCard);
            return;
        }

        if (manfred.playerInput.GetDidPressSiphon())
        {
            manfred.fsm.ChangeState(manfred.stateSiphonStart, manfred.stateSiphonStart);
            return;
        }

        // check if hit ground
        CharacterController2D.CharacterCollisionState2D collisions = manfred.controller.collisionState;
        if (collisions.below)
        {
            manfred.velocity.y = 0f;
            SpawnLandEffect();
            manfred.fsm.ChangeState(manfred.stateGrounded, manfred.stateGrounded);
            return;
        }

        // check if hit ground
        if (collisions.left && horizInput < 0 || collisions.right && horizInput > 0)
        {
            manfred.velocity.x = 0f;
            manfred.velocity.y = 0f;
            bool isWallOnLeft = collisions.left;
            manfred.fsm.ChangeState(manfred.stateWallCling, manfred.stateWallCling, isWallOnLeft);
            return;
        }
    }
Exemple #14
0
    void Update()
    {
        animator.SetFloat("movementX", Mathf.Abs(moveDirection.x / walkSpeed));       // +Normalizado
        animator.SetFloat("movementY", moveDirection.y);
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isJumping", isJumping);
        animator.SetBool("isDucking", isDucking);
        animator.SetBool("isFalling", isFalling);
        animator.SetBool("isAttacking", isAttacking);
        animator.SetBool("isDead", isDead);
        animator.SetBool("isClimbing", isClimbing);


        if (moveDirection.y < 0)
        {
            isFalling = true;
        }
        else
        {
            isFalling = false;
        }

        moveDirection.x  = Input.GetAxis("Horizontal"); // recupera valor dos controles
        moveDirection.x *= walkSpeed;

        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            isFacingRight         = false;
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            isFacingRight         = true;
        }

        if (Input.GetButton("Fire1"))
        {
            //audioSource.PlayOneShot(attackSound);
            //audioSource.PlayOneShot(swordSound);
            isAttacking        = true;
            boxCollider.size   = new Vector2(2 * colliderSizeX, colliderSizeY);
            boxCollider.offset = new Vector2(colliderOffsetX, colliderOffsetY);
            characterController.recalculateDistanceBetweenRays();
        }
        else
        {
            isAttacking        = false;
            boxCollider.size   = new Vector2(colliderSizeX, colliderSizeY);
            boxCollider.offset = new Vector2(colliderOffsetX, colliderOffsetY);
            characterController.recalculateDistanceBetweenRays();
        }

        if (isGrounded)                       // caso esteja no chão
        {
            moveDirection.y = 0.0f;           // se no chão nem subir nem descer
            isJumping       = false;
            doubleJumped    = false;          // se voltou ao chão pode faz pulo duplo
            if (Input.GetButton("Jump"))
            {
                audioSource.PlayOneShot(jumpSound);
                moveDirection.y = jumpSpeed;
                isJumping       = true;
            }
        }
        else              // caso esteja pulando

        {
            if (Input.GetButtonUp("Jump") && moveDirection.y > 0)            // Soltando botão diminui pulo
            {
                moveDirection.y *= 0.5f;
            }

            if (Input.GetButtonDown("Jump") && !doubleJumped)            // Segundo clique faz pulo duplo
            {
                moveDirection.y = doubleJumpSpeed;
                audioSource.PlayOneShot(jumpSound);
                doubleJumped = true;
            }
        }

        moveDirection.y -= gravity * Time.deltaTime;            // aplica a gravidade

        animator.speed = 1.0f;
        if (isClimbing)
        {
            Debug.Log(Input.GetAxis("Vertical"));
            if (Input.GetAxis("Vertical") > 0)
            {
                moveDirection.y = walkSpeed;
                isJumping       = true;
            }
            else if (Input.GetAxis("Vertical") < 0)
            {
                moveDirection.y = -walkSpeed;
            }
            else
            {
                if (!isGrounded)
                {
                    Debug.Log("entrou aqui");
                    moveDirection.y = 0.0f;
                    animator.speed  = 0.0f;
                }
            }
        }


        characterController.move(moveDirection * Time.deltaTime); // move personagem

        flags      = characterController.collisionState;          // recupera flags
        isGrounded = flags.below;                                 // define flag de chão

        if (Input.GetAxis("Vertical") < 0 && moveDirection.x == 0)
        {
            if (!isDucking)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, 2 * colliderSizeY / 3);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY - colliderSizeY / 6);
                characterController.recalculateDistanceBetweenRays();
            }
            isDucking = true;
        }
        else
        {
            if (isDucking)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, colliderSizeY);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY);
                characterController.recalculateDistanceBetweenRays();
                isDucking = false;
            }
        }
    }
    void Update()
    {
        if (isDead)
        {
            return;
        }
        if (jumpPower)
        {
            if (Input.GetKeyDown("page up"))
            {
                Debug.Log("Got +");
                if (jumpXBoost < 2f)
                {
                    jumpXBoost += .5f;
                    jumpNo.SendMessage("change", jumpXBoost);
                }
            }
            if (Input.GetKeyDown("page down"))
            {
                Debug.Log("Got -");
                if (jumpXBoost > 0f)
                {
                    jumpXBoost -= .5f;
                    jumpNo.SendMessage("change", jumpXBoost);
                }
            }
        }


        horizontal_input = Input.GetAxis("Horizontal");
        if (horizontal_input != 0)
        {
            if (horizontal_input > 0)
            {
                x_direction = 1f;
            }
            else
            {
                x_direction = -1f;
            }
        }

        if (x_direction < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }
        else
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }

        if (isGrounded)                                 // caso esteja no chão
        {
            if (isJumping)
            {
                flying_speed = 0.0f;
                jumpSpeed    = 0f;
                audioManager.Play("fall");
                isJumping = false;
            }
            else
            {
                if (!isCharging)
                {
                    moveDirection.x  = horizontal_input;                    // recupera valor dos controles
                    moveDirection.x *= walkSpeed;
                }
            }


            if (Input.GetButtonDown("Jump"))
            {
                audioManager.Play("charge");
                isCharging      = true;
                jumpSpeed       = 1f;
                moveDirection.x = 0;
            }
            if (isCharging)
            {
                if (Input.GetButton("Jump"))
                {
                    moveDirection.x = 0;
                    jumpSpeed      *= 1f + chargeSpeed;
                }
                else
                {
                    isCharging = false;
                }
            }
            if (Input.GetButtonUp("Jump"))
            {
                audioManager.Play("jump");
                if (jumpSpeed > maxjumpSpeed)
                {
                    jumpSpeed = maxjumpSpeed;
                }
                moveDirection.y = jumpSpeed;
                flying_speed    = x_direction * (jumpSpeed * jumpXModifier + jumpXBoost);
                moveDirection.x = flying_speed;
                isJumping       = true;
                isCharging      = false;
            }
            //Se comecou a cair mas nao tava pulando
        }
        else if (wasGroundedLastFrame && !isJumping)
        {
            moveDirection.y = 0f;             //
        }
        else if (isJumping)
        {
            moveDirection.x = flying_speed;
        }
        if (gotFlower)
        {
            moveDirection.x  = horizontal_input;            // recupera valor dos controles
            moveDirection.x *= walkSpeed;
        }
        if (hitHead)
        {
            if (isJumping)
            {
                moveDirection.y = 0f;
            }
        }

        isWalking = (Mathf.Abs(moveDirection.x) > .2) && !isJumping;

        animator.SetBool("charging", isCharging);
        animator.SetBool("jumping", isJumping);
        animator.SetBool("walking", isWalking);
        animator.SetFloat("y_speed", moveDirection.y);

        moveDirection.y -= gravity * Time.deltaTime;            // aplica a gravidade
        if (Mathf.Abs(moveDirection.y) > gravity)
        {
            moveDirection.y = -gravity;
        }
        flags    = characterController.collisionState;          // recupera flags
        hitLeft  = flags.left;
        hitRight = flags.right;
        if (hitLeft || hitRight)         //provavelmente n ta fazendo nada, mas talvez esteja e ta parecendo bom :)
        {
            flying_speed = -flying_speed / 2;
        }
        characterController.move(moveDirection * Time.deltaTime);  // move personagem

        flags                = characterController.collisionState; // recupera flags
        isGrounded           = flags.below;                        // define flag de chão
        wasGroundedLastFrame = flags.wasGroundedLastFrame;
        hitHead              = flags.above;
    }
    // Update is called once per frame
    void Update()
    {
        GetGroundType();

        jumpPressedRemember -= Time.deltaTime;
        groundedRemember    -= Time.deltaTime;

        if (_wallJumped == false)
        {
            _moveDirection.x  = Input.GetAxis("Horizontal");
            _moveDirection.x *= _walkSpeed;

            _verticalInput = Input.GetAxis("Vertical");
        }

        if (Input.GetButtonDown("Jump"))
        {
            jumpPressedRemember = jumpPressedRememberTime;
        }

        // Is Player on the ground
        if (_isGrounded)
        {
            // Values to reset once grounded
            GroundVariableReset();

            groundedRemember = groundedRememberTime;

            // Change is facing value depending on moving direction
            if (_moveDirection.x > 0)
            {
                RotatePlayer("right");
            }
            else if (_moveDirection.x < 0)
            {
                RotatePlayer("left");
            }
            // IDLE
            else
            {
            }

            if (_verticalInput < 0)
            {
                if (_groundType == groundType.OneWayPlatform)
                {
                    print("DO IT");
                    StartCoroutine(DisableOneWayPlatform());
                }
            }

            // Jump
            if (jumpPressedRemember > 0 && groundedRemember > 0)
            {
                Jump();
            }
        }

        // Is Player in the air
        else
        {
            // Variable Resets
            _isWallSliding = false;

            if (_moveDirection.x > 0)
            {
                RotatePlayer("right");
            }
            else if (_moveDirection.x < 0)
            {
                RotatePlayer("left");
            }
            else
            {
                // IDLE
            }


            // Did the player let go of jump button
            if (Input.GetButtonUp("Jump"))
            {
                if (_moveDirection.y > 0)
                {
                    _moveDirection.y = _moveDirection.y * 0.4f;
                }
            }

            // Did player press jump
            if (Input.GetButtonDown("Jump"))
            {
                // has the ability to double jump..
                if (_enableDoubleJump)
                {
                    // hasn't double jumped already
                    if (!_doubleJumping)
                    {
                        DoubleJump();
                    }
                }
            }
        }

        _characterController.move(_moveDirection * Time.deltaTime);

        // Checking Player flags/Collision states
        flags       = _characterController.collisionState;
        _isGrounded = flags.below;
        // Hopefully this doesn't bite me in the ass later :'(

        /*if (_moveDirection.y != 0 || _moveDirection.x != 0)//
         * {
         *  _isGrounded = flags.below;
         * }*/

        if (!_isGrounded)
        {
            if (_moveDirection.y > -60)
            {
                _moveDirection.y -= (_gravity * Time.deltaTime) * _gravityAcceleration;
            }
            else
            {
                _moveDirection.y = -60;
            }
        }
        //moveDirection.y -= (gravity * Time.deltaTime) * 2.1f;

        //Is there anything above the player
        if (flags.above)
        {
            _moveDirection.y -= _gravity * Time.deltaTime;
        }

        // Is there anything to the left or right of the player
        if (flags.left || flags.right)
        {
            if (_enableWallRun)
            {
                WallSlide();

                WallRun();
            }
            if (_enableWallJump)
            {
                if (Input.GetButtonDown("Jump") && !_wallJumped && !_isGrounded)
                {
                    WallJump();
                }
            }
        }
        // if there is nothing to the left or right of the player
        else
        {
            // variable reset
            _isWallSliding = false;

            if (_enableWallRunAfterWallJump)
            {
                StopCoroutine("WallRunTimer");
                _canWallRun = true;
            }

            _isWallRunning = false;
            // No longer touching well, therefore set previous wall to equal the wall that was just run/jumped from
            previousWallSideNumber = currentWallSideNumber;
        }
    }
Exemple #17
0
    void Update()
    {
        moveDirection.x  = Input.GetAxis("Horizontal"); // recupera valor dos controles
        moveDirection.x *= walkSpeed;

        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            isFacingRight         = false;
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            isFacingRight         = true;
        }

        if (Input.GetButton("Fire1"))
        {
            isAttacking = true;
        }
        else
        {
            isAttacking = false;
        }

        if (isGrounded)
        {               // caso esteja no chão
            moveDirection.y = 0.0f;
            isJumping       = false;
            doubleJumped    = false; // se voltou ao chão pode faz pulo duplo
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
                isJumping       = true;
            }
        }
        else
        {                                                         // caso esteja pulando
            if (Input.GetButtonUp("Jump") && moveDirection.y > 0) // Soltando botão diminui pulo
            {
                moveDirection.y *= 0.5f;
            }
            if (Input.GetButtonDown("Jump") && !doubleJumped) // Segundo clique faz pulo duplo
            {
                moveDirection.y = doubleJumpSpeed;
                doubleJumped    = true;
            }
        }

        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 4f, mask);

        if (hit.collider != null && isGrounded)
        {
            transform.SetParent(hit.transform);
            if (Input.GetAxis("Vertical") < 0 && Input.GetButtonDown("Jump"))
            {
                moveDirection.y = -jumpSpeed;
                StartCoroutine(PassPlatform(hit.transform.gameObject));
            }
        }
        else
        {
            transform.SetParent(null);
        }


        moveDirection.y -= gravity * Time.deltaTime;              // aplica a gravidade
        characterController.move(moveDirection * Time.deltaTime); // move personagem

        flags      = characterController.collisionState;          // recupera flags
        isGrounded = flags.below;                                 // define flag de chão


        // Atualizando Animator com estados do jogo
        animator.SetFloat("movementX", Mathf.Abs(moveDirection.x / walkSpeed)); // +Normalizado
        animator.SetFloat("movementY", moveDirection.y);
        animator.SetBool("isGrounded", isGrounded);
        animator.SetBool("isJumping", isJumping);
        animator.SetBool("isAttacking", isAttacking);
        animator.SetBool("isDying", isDying);

        if (PlayerPrefs.GetInt("Vidas", 0) < 0)
        {
            SceneManager.LoadScene("GameOver");
        }
    }
    void Update()
    {
        moveDirection.x  = Input.GetAxis("Horizontal"); // recupera valor dos controles
        moveDirection.x *= walkSpeed;

        // Conforme direção do personagem girar ele no eixo Y
        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
            isFacingRight         = false;
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
            isFacingRight         = true;
        }                            // se direção em x == 0 mantenha como está a rotação

        if (isGrounded)              // caso esteja no chão
        {
            moveDirection.y = 0.0f;  // se no chão nem subir nem descer
            isJumping       = false;
            doubleJumped    = false; // se voltou ao chão pode faz pulo duplo
            if (Input.GetButton("Jump"))
            {
                moveDirection.y = jumpSpeed;
                isJumping       = true;
            }
        }
        else                                                      // caso esteja pulando
        {
            if (Input.GetButtonUp("Jump") && moveDirection.y > 0) // Soltando botão diminui pulo
            {
                moveDirection.y *= 0.5f;
            }

            if (Input.GetButtonDown("Jump") && !doubleJumped)            // Segundo clique faz pulo duplo
            {
                moveDirection.y = doubleJumpSpeed;
                doubleJumped    = true;
            }
        }


        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 4f, mask);

        if (hit.collider != null && isGrounded)
        {
            transform.SetParent(hit.transform);
            if (Input.GetAxis("Vertical") < 0 && Input.GetButtonDown("Jump"))
            {
                moveDirection.y = -jumpSpeed;
                StartCoroutine(PassPlatform(hit.transform.gameObject));
            }
        }
        else
        {
            transform.SetParent(null);
        }

        if (moveDirection.y < 0)
        {
            isFalling = true;
        }
        else
        {
            isFalling = false;
        }

        moveDirection.y -= gravity * Time.deltaTime;                                    // aplica a gravidade

        animator.speed = 1.0f;
        if (isClimbing)
        {
            if (Input.GetAxis("Vertical") > 0)
            {
                moveDirection.y = walkSpeed;
                isJumping       = true;
            }
            else if (Input.GetAxis("Vertical") < 0)
            {
                moveDirection.y = -walkSpeed;
            }
            else
            {
                if (!isGrounded)
                {
                    moveDirection.y = 0.0f;
                    animator.speed  = 0.0f;
                }
            }
        }

        characterController.move(moveDirection * Time.deltaTime);               // move o personagem

        flags      = characterController.collisionState;                        // recupera flags
        isGrounded = flags.below;                                               // define flag de chão

        if (Input.GetAxis("Vertical") < 0 && moveDirection.x == 0)
        {
            if (!isDucking)
            {
                boxCollider.size   = new Vector2(boxCollider.size.x, 2 * colliderSizeY / 3);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY - colliderSizeY / 6);
                characterController.recalculateDistanceBetweenRays();
            }
            isDucking = true;
        }
        else
        {
            if (isDucking)
            {
                isDucking          = false;
                boxCollider.size   = new Vector2(boxCollider.size.x, colliderSizeY);
                boxCollider.offset = new Vector2(boxCollider.offset.x, colliderOffsetY);
                characterController.recalculateDistanceBetweenRays();
            }
        }

        UpdateAnimator();
    }
Exemple #19
0
    void Update()
    {
        if (!hasWallJumped)
        {
            moveDirection.x  = Input.GetAxis("Horizontal");
            moveDirection.x *= walkSpeed;             // multiply by walkspeed
        }

        if (isGrounded)
        {
            anim.SetBool("isJumping", false);

            // is on the ground
            moveDirection.y = 0;

            isJumping       = false;
            hasDoubleJumped = false;

            anim.SetFloat("Running", Mathf.Abs(moveDirection.x));

            // jump management
            if (Input.GetButtonDown("Jump"))
            {
                anim.SetBool("isJumping", true);
                sound.Play();
                moveDirection.y = jumpSpeed;
                isJumping       = true;

                isWallRunning = true;
            }
        }
        else
        {
            // jump button held
            if (Input.GetButtonUp("Jump"))
            {
                if (moveDirection.y > 0)
                {
                    moveDirection.y = moveDirection.y * 0.5f;
                }
            }

            // Ability : Double Jump
            if (Input.GetButtonDown("Jump"))
            {
                if (canDoubleJump && !hasDoubleJumped)
                {
                    moveDirection.y = doubleJumpSpeed;
                    hasDoubleJumped = true;
                }
            }
        }

        // direction management
        if (moveDirection.x < 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }
        else if (moveDirection.x > 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }

        // fall rate
        moveDirection.y -= gravity * Time.deltaTime;

        // moving left or right (or jump or fall)
        charCtrl2D.move(moveDirection * Time.deltaTime);
        // report what is around us
        flags = charCtrl2D.collisionState;

        // check if we are on the ground
        isGrounded = flags.below;

        // check if we hit our head
        if (flags.above)
        {
            moveDirection.y -= gravity * Time.deltaTime;
        }

        if (flags.left || flags.right)
        {
            // Ability : Wall Run
            if (canWallRun)
            {
                if (Input.GetAxis("Vertical") > 0 && isWallRunning)
                {
                    moveDirection.y = jumpSpeed / wallJumpAmount.y;
                    StartCoroutine(WallRunWaiter(0.05f));
                }
            }

            // Ability : Wall Jump
            if (canWallJump)
            {
                if (Input.GetButtonDown("Jump") && !hasWallJumped && !isGrounded)
                {
                    if (moveDirection.x < 0)
                    {
                        moveDirection.x       = jumpSpeed * wallJumpAmount.x;
                        moveDirection.y       = jumpSpeed * wallJumpAmount.y;
                        transform.eulerAngles = new Vector3(0, 0, 0);
                        //lastJumpWasLeft = false;
                    }
                    else if (moveDirection.x > 0)
                    {
                        moveDirection.x       = -jumpSpeed * wallJumpAmount.x;
                        moveDirection.y       = jumpSpeed * wallJumpAmount.y;
                        transform.eulerAngles = new Vector3(0, 180, 0);
                        //lastJumpWasLeft = true;
                    }

                    StartCoroutine(HasWallJumpedTimer(wallJumpWait));
                }
            }
        }
    }