Пример #1
0
        void FixedUpdate()
        {
            Grounded = Physics2D.OverlapCircle (GroundCheck.position, GroundedCheckRadius, whatIsGround);

            if (Jump && !JumpFromBeginningToEnd && Grounded && !JumpInProgress) {

                JumpFromBeginningToEnd = true;
                Jump = false;
                JumpInProgress = true;
                StopWalking = true;

                // Get actual position
                highestJumpCoordinate = gameObject.transform.position.y;
                // and fix it
                highestJumpCoordinate -= 0.1f;

                // Jump up and forward
                if(EnemyState.Equals(EnemyStates.JUMP_FORWARD)){

                    GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
                    GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpForceUpForward);

                    // Save as temp variable
                    float JumpForwardSpeedTempValue = MaxSpeed;

                    if(!facingRight){
                        JumpForwardSpeedTempValue = JumpForwardSpeedTempValue * -1;
                    }

                    GetComponent<Rigidbody2D>().velocity = new Vector2( JumpForwardSpeedTempValue, GetComponent<Rigidbody2D>().velocity.y );

                    // Jump up
                } else if(EnemyState.Equals(EnemyStates.JUMP_UP)){

                    GetComponent<Rigidbody2D>().velocity = new Vector2(0,0);
                    GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpForceUp);

                }
            }

            if (JumpInProgress) {

                // This is the jumping process. If the enemy should jump through walls the
                // ground layer will be disable on up-jump and will be enabled when the enemy arrive the highest jump-point
                float actualY = gameObject.transform.localPosition.y;

                if(actualY > highestJumpCoordinate){
                    highestJumpCoordinate = actualY;

                    if(JumpThroughWalls){
                        EnemyJumpCollider.enabled = false;
                    }

                } else {

                    if(JumpThroughWalls){
                        EnemyJumpCollider.enabled = true;
                    }

                    JumpInProgress = false;

                    // Wait a little bit to continue auto walking
                    StartCoroutine(ResumeAutoWalkAfterTime(0.2f));
                }

            } else {

                // Auto walk when grounded
                if(!StopWalking && Grounded){

                    // Save x value to for checking flip
                    horizontalPositionOld = transform.position.x;

                    if(moveRight) {
                        transform.Translate(Vector3.right * Time.deltaTime * MaxSpeed, Space.World);
                    } else {
                        transform.Translate(-Vector3.right * Time.deltaTime * MaxSpeed, Space.World);
                    }

                    // Enemy schaut nach links bzw. nach rechts
                    if(transform.position.x < horizontalPositionOld){
                        // rechts
                        if(facingRight){
                            Flip ();
                            facingRight = false;
                        }
                    } else {
                        if(!facingRight){
                            Flip ();
                            facingRight = true;
                        }
                    }

                    // If EnemyAutoJumpUp activated the jump in certain intervals
                    EnemyAutoJumpUpAfterTime();
                }
            }
        }