Esempio n. 1
0
    // Check inputs every frame
    void Update()
    {
        //Reset position if falling off world
        if (this.transform.position.y < -7)
        {
            this.transform.position = new Vector3(5, 10, 0);
        }

        //Character swapping
        if (inputMap.ChangeToAlien())
        {
            rb.rotation = 0f;
            canMove     = false;
            _animator.Play(Animator.StringToHash("ShipIdle"));
            rb.isKinematic    = false;
            rb.freezeRotation = true;
        }
        else if (inputMap.ChangeToShip())
        {
            _animator.Play(Animator.StringToHash("Fly"));
            canMove           = true;
            rb.isKinematic    = true;
            rb.freezeRotation = false;
        }

        //Movement and Actions
        if (canMove)
        {
            if (inputMap.GoUp())
            {
                rb.rotation = 0f;
                transform.Translate(Vector2.up * 0.05f, Space.World);
            }
            else if (inputMap.GoDown())
            {
                rb.rotation = 0f;
                transform.Translate(Vector2.down * 0.05f, Space.World);
            }

            if (inputMap.GoLeft())
            {
                rb.rotation = 8f;
                transform.Translate(Vector2.left * 0.05f, Space.World);
            }
            else if (inputMap.GoRight())
            {
                rb.rotation = -8f;
                transform.Translate(Vector2.right * 0.05f, Space.World);
            }
            else if (inputMap.GoHorizontalAnalog() != 0f)
            {
                float speed = inputMap.GoHorizontalAnalog();
                transform.Translate(Vector2.right * 0.06f * speed, Space.World);
                rb.rotation = speed * -8;
            }
            else
            {
                rb.rotation = 0f;
            }

            if (inputMap.ShootCoinDown())
            {
                Rigidbody2D coinInstance;
                coinInstance = Instantiate(coinPrefab, (this.transform.position + new Vector3(0, -1f, 0)), this.transform.rotation) as Rigidbody2D;
                coinInstance.AddForce(-5 * rb.transform.up, ForceMode2D.Impulse);
            }

            if (inputMap.ShootCoinUp())
            {
                Rigidbody2D coinInstance;
                coinInstance = Instantiate(coinPrefab, (this.transform.position + new Vector3(0, 1f, 0)), this.transform.rotation) as Rigidbody2D;
                coinInstance.AddForce(5 * rb.transform.up, ForceMode2D.Impulse);
            }
        }
    }
Esempio n. 2
0
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    void Update()
    {
        //Choose between Alien and Spaceship
        if (inputMap.ChangeToAlien())
        {
            canMove = true;
        }
        else if (inputMap.ChangeToShip())
        {
            canMove = false;
        }

        //Reset position if falling off world
        if (this.transform.position.y < -7)
        {
            this.transform.position = new Vector3(0, 10, 0);
        }

        if (canMove)
        {
            if (_controller.isGrounded)
            {
                _velocity.y = 0;
            }

            if (inputMap.GoRight())
            {
                normalizedHorizontalSpeed = 1;
                if (transform.localScale.x < 0f)
                {
                    transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                }

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else if (inputMap.GoLeft())
            {
                normalizedHorizontalSpeed = -1;
                if (transform.localScale.x > 0f)
                {
                    transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                }

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else if (inputMap.GoHorizontalAnalog() != 0f)
            {
                normalizedHorizontalSpeed = inputMap.GoHorizontalAnalog();

                transform.localScale = new Vector3(Mathf.Sign(normalizedHorizontalSpeed), transform.localScale.y, transform.localScale.z);

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else
            {
                normalizedHorizontalSpeed = 0;

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Idle"));
                }
            }

            if (inputMap.AttackNow() && !isAttacking)
            {
                isAttacking = true;
                StartCoroutine(WaitAttack());
                _animator.Play(Animator.StringToHash("Attack"));
            }

            // we can only jump whilst grounded
            if ((_controller.isGrounded || isClimbing) && inputMap.JumpNow())
            {
                _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
                _animator.Play(Animator.StringToHash("Jump"));
            }

            //Climbing stairs (improvised code just to display the 'moving up' control input. Don't use it for your project)
            if (canClimb && inputMap.GoUp())
            {
                isClimbing = true;
            }

            if (isClimbing)
            {
                _velocity.y = 0f;;
            }

            if (isClimbing && inputMap.GoUp())
            {
                _animator.Play(Animator.StringToHash("Climb"));
                _velocity.y = 3f;
            }

            if (isClimbing && inputMap.GoDown())
            {
                _animator.Play(Animator.StringToHash("Climb"));
                _velocity.y = -3f;
            }

            if (inputMap.JumpNow())
            {
                isClimbing = false;
            }  //End of climbing hack code

            // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
            var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
            _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

            // apply gravity before moving (only if not climbing
            if (!isClimbing)
            {
                _velocity.y += gravity * Time.deltaTime;
            }

            // if holding down bump up our movement amount and turn off one way platform detection for a frame.
            // this lets uf jump down through one way platforms
            if (_controller.isGrounded && inputMap.GoDown())
            {
                _velocity.y *= 3f;
                _controller.ignoreOneWayPlatformsThisFrame = true;
            }

            _controller.move(_velocity * Time.deltaTime);

            // grab our current _velocity to use as a base for all calculations
            _velocity = _controller.velocity;
        }
    }