Esempio n. 1
0
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    IEnumerator CharacterUpdate()
    {
        while (true)
        {
            if (!effected)
            {
                if (Input.GetAxis("Horizontal") != 0)
                {
                    if (!_as.isPlaying)
                    {
                        _as.volume = 1;
                        _as.Play();
                    }

                    if ((RB_PC.velocity.x > 0 && Input.GetAxis("Horizontal") < 0) || (RB_PC.velocity.x < 0 && Input.GetAxis("Horizontal") > 0))
                    {
                        RB_PC.velocity = new Vector2(0, RB_PC.velocity.y);
                        EngineSound.SetActive(true);
                    }
                }
                else
                {
                    if (_as.isPlaying)
                    {
                        if (_as.volume != 0)
                        {
                            _as.volume = _as.volume - 0.05f;
                        }
                        else
                        {
                            _as.Stop();
                        }
                    }
                }


                if (Mathf.Abs(RB_PC.velocity.x) < fl_move_speed)
                {
                    RB_PC.AddForce(new Vector2(Input.GetAxis("Horizontal") * fl_acceleration, 0));
                }
            }


            // we can only jump whilst grounded
            if (_controller.isGrounded && wantJump && vacuum_attached)
            {
                wantJump = false;
                if (!firingTrigger)
                {
                    if (theVacuum.vacuumRaycastHit)
                    {
                        RB_PC.velocity = _velocity;

                        //if (theVacuum.contactHit.x > gameObject.transform.position.x)
                        //{
                        //    _velocity.x = -Mathf.Sqrt(2f * jumpHeight);
                        //    //_animator.Play( Animator.StringToHash( "Jump" ) );
                        //    if (jumpSound)
                        //        jumpSound.Play();
                        //}

                        //if (theVacuum.contactHit.x < gameObject.transform.position.x)
                        //{
                        //    _velocity.x = Mathf.Sqrt(2f * jumpHeight);
                        //    //_animator.Play( Animator.StringToHash( "Jump" ) );
                        //    if (jumpSound)
                        //        jumpSound.Play();
                        //}

                        if (theVacuum.contactHit.y > gameObject.transform.position.y)
                        {
                            _velocity.y = -Mathf.Sqrt(2f * jumpHeight);
                            //_animator.Play( Animator.StringToHash( "Jump" ) );
                            if (jumpSound)
                            {
                                jumpSound.Play();
                            }
                        }

                        if (theVacuum.contactHit.y < gameObject.transform.position.y)
                        {
                            _velocity.y = Mathf.Sqrt(2f * jumpHeight);
                            //_animator.Play( Animator.StringToHash( "Jump" ) );
                            if (jumpSound)
                            {
                                jumpSound.Play();
                            }
                        }
                    }

                    yield return(new WaitForEndOfFrame());
                }
                else
                {
                    // Switch level
                    Debug.Log("Load next Level");
                    loadNextLevel();
                }
            }

            if (firingDead)
            {
                reloadLevel();
            }

            //var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
            //_velocity.x = Mathf.Lerp( _velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor );

            // 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 && Input.GetKey(KeyCode.DownArrow))
            {
                _controller.ignoreOneWayPlatformsThisFrame = true;
            }

            yield return(new WaitForEndOfFrame());

            _controller.move(RB_PC.velocity * Time.deltaTime);

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