Ejemplo n.º 1
0
 public void Move(Vector2 direction)
 {
     if (direction.x != 0 && !isJumpInCooldown && Physics2DUtility.IsOnGround(transform.position, myCollider))
     {
         myRigidbody.AddForce(new Vector2(1 * Mathf.Sign(direction.x), 1) * jumpForce, ForceMode2D.Impulse);
         isJumpInCooldown = true;
         Invoke("EnableJump", jumpCooldown);
     }
 }
    void Update()
    {
        var xVelocity = myRigidbody.velocity.x;

        if (Math.Abs(xVelocity) > VelocityFlipTrigger)
        {
            // reverse object
            var newScale = transform.localScale;
            if (Mathf.Sign(newScale.x) != Mathf.Sign(xVelocity))
            {
                newScale.x *= -1;
            }
            transform.localScale = newScale;
        }

        animator.SetBool("isOnGround", Physics2DUtility.IsOnGround(transform.position, myCollider));
    }
Ejemplo n.º 3
0
    void FixedUpdate()
    {
        bool isOnGround          = Physics2DUtility.IsOnGround(transform.position, myCollider);
        bool isAffectedByGravity = myRigidbody.gravityScale > 0;

        // Jump take-off
        if (startJump && (isOnGround || !isAffectedByGravity) && !isOnCooldown)
        {
            Vector2 jumpDirection, finalJumpForce;
            if (isAffectedByGravity)
            {
                jumpDirection  = new Vector2(direction.x, 1);
                finalJumpForce = jumpForce;
            }
            else
            {
                jumpDirection = direction;
                if (jumpDirection == Vector2.zero)
                {
                    jumpDirection = Vector2.up;
                }
                finalJumpForce = new Vector2(jumpForce.y, jumpForce.y);
            }

            myRigidbody.AddForce(jumpDirection * finalJumpForce, ForceMode2D.Impulse);
            OnJump?.Invoke();

            isOnCooldown = true;
            Invoke(nameof(EnableJump), cooldownTime);
        }

        // Jump maneuvers
        if (!isOnGround && isAffectedByGravity)
        {
            myRigidbody.AddForce(Vector2.right * direction.x * lateralForce);

            if (extendJump && myRigidbody.velocity.y > 0)
            {
                myRigidbody.AddForce(Vector2.up * extendForce);
            }
        }

        startJump  = false;
        extendJump = false;
    }
    // TODO: Fix animation error when leaving object
    void Update()
    {
        var inputDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        if (inputDirection.x != 0)
        {
            // reverse object
            var newScale = transform.localScale;
            if (Mathf.Sign(newScale.x) != Mathf.Sign(inputDirection.x))
            {
                newScale.x *= -1;
            }
            transform.localScale = newScale;

            //animator.SetTrigger("move");
        }
        else
        {
            animator.SetTrigger("stop");
        }

        animator.SetBool("isOnGround", Physics2DUtility.IsOnGround(transform.position, myCollider));
        animator.SetFloat("verticalSpeed", myRigidbody.velocity.y);
    }