コード例 #1
0
        private void FixedUpdate()
        {
            // print("fixed update" + _jumping.ToString() + _grounded.ToString());
            _xVel = GetComponent <Rigidbody2D>().velocity.x;
            _yVel = GetComponent <Rigidbody2D>().velocity.y;

            _grounded = IsGrounded();

            if (_groundRigidBody != null && !_grounded)
            {
                _groundRigidBody = null;
            }
            // Process Horizontal
            if (InputWrapper.Instance.GetRigth())
            {
                _xVel = 1 * MaxSpeed;
            }
            else if (InputWrapper.Instance.GetLeft())
            {
                _xVel = -1 * MaxSpeed;
            }
            else
            {
                _xVel = 0;
            }
            if ((_xVel > 0 && !_facingRight) || (_xVel < 0 && _facingRight))
            {
                Flip();
            }
            _xVel += PlatformVelocity().x;

            // Process Vertical
            if (_grounded)
            {
                _yVel       = PlatformVelocity().y - 0.01f;            // maintain velocity of platform, with slight downward pressure to keep the collision.
                _doubleJump = true;
            }
            if (_jumping && _grounded)
            {
                _yVel = JumpSpeed;
                print("Jump?");

                //AudioPlayer.Instance.PlaySfx (JumpFx);
                randomC.clips = JumpFx;
                randomC.PlaySound();
            }
            else if (_jumping && _doubleJump)
            {
                _yVel       = JumpSpeed;
                _doubleJump = false;
                print("Jump?");
                //AudioPlayer.Instance.PlaySfx (JumpFx);
                randomC.clips = JumpFx;
                randomC.PlaySound();
            }
            _jumping = false;

            GetComponent <Rigidbody2D>().velocity = new Vector2(_xVel, _yVel);
            UpdateAnimationParams();
        }
コード例 #2
0
    void Update()
    {
        Application.targetFrameRate = 60;

        //If the game is over or the game has not started, don't apply physics.
        if (gameOver || !gameStarted)
        {
            return;
        }

        if (Input.GetKeyDown(jumpKey))
        {
            //Sets the rotation of our object to 20 degrees, the slerp at the bottom of this function will be what tilts us back down.
            transform.rotation = Quaternion.Euler(0, 0, 20f);

            //Sets the Rigidbody to a zero velocity.
            rigidbody2D.velocity = Vector2.zero;

            //Adds an upwards force.
            rigidbody2D.AddForce(Vector2.up * jumpHeight);

            randCont.clips = flapclips;

            randCont.PlaySound(false);

            //Attemps to do a jump animation on our set animator.
            if (animator)
            {
                try
                {
                    //Sets the jump trigger on the animator.
                    animator.SetTrigger("Jump");
                }
                catch (System.Exception ex) //Catches if the triggers are not set correctly.
                {
                    Debug.LogError("You have not configured your animator trigger event correctly. Please make sure you have a `jump` trigger or modify this code.");
                }
            }
            else
            {
                Debug.LogError("You have no attached an animator to your BirdController or have not set the property. Please make sure you have one or you will not have working animations.");
            }
        }

        //Apply downwards gravity.
        rigidbody2D.AddForce(Physics2D.gravity * gravityScale);
        //Modify the rotation of our player to be a -32 degrees on the Z-axis
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, 0, -32f), 0.05f);
    }
コード例 #3
0
    public void ResetGame()
    {
        randCont.clips = buttonclips;

        randCont.PlaySound(false);

        //Reload the scene.
        UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().name);
    }
コード例 #4
0
    // Update is called once per frame
    void Update()
    {
        if (playing)
        {
            float lastScore = CurrentScore;
            CurrentScore += ScrollSpeed / 250f;
            // to tell when it passes a 100 score benchmark
            if (CurrentScore % 100 < lastScore % 100)
            {
                ScrollSpeed *= 1.03f;
                if (cactusRate > 0.5f)
                {
                    cactusRate *= 0.95f;
                    print("cactus rate is now " + cactusRate);
                }
                randomCon.clips = scoreClips;
                randomCon.PlaySound();
            }
            // change seasons every 500 score
            if (CurrentScore % seasonInterval < lastScore % seasonInterval)
            {
                isSummer = !isSummer;
            }
            if ((CurrentScore - 17f) % seasonInterval < (lastScore - 17f) % seasonInterval)
            {
                ChangeSeason();
            }
            string scoreString = "";
            for (int i = 1; i <= 5 - ((int)CurrentScore).ToString().Length; i++)
            {
                scoreString += '0';
            }
            scoreString += ((int)CurrentScore).ToString();
            Score.GetComponent <Text>().text = scoreString;

            HighScore += ScrollSpeed / 250f;
            string highscoreString = "HI ";
            for (int i = 1; i <= 5 - ((int)HighScore).ToString().Length; i++)
            {
                highscoreString += '0';
            }
            highscoreString += ((int)HighScore).ToString();
            HighestScore.GetComponent <Text>().text = highscoreString;
        }
        else
        {
            if (Input.GetKey("space"))
            {
                Resume();
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }
        }
    }
コード例 #5
0
    void Jump()
    {
        if (dead != true)
        {
            //creates new vector for jump movement
            //SoundManager.PlaySound("Jump");

            Debug.Log("JUMP CLIPS SIZE: " + jumpClips.Length);

            Vector3 jumpMovement = new Vector3(0.0f, 1.0f, 0.0f);
            //sets player velocity to jumpmovement * jumpspeed
            myRB.velocity = jumpMovement * jumpSpeed;
            grounded      = false;
            randomC.clips = jumpClips;
            randomC.PlaySound(false);
        }
    }
コード例 #6
0
 // Update is called once per frame
 void FixedUpdate()
 {
     if (GameManager.GetPlaying())
     {
         if ((Input.GetKey(Jump[0]) || Input.GetKey(Jump[1])) && !Input.GetKey("down") && canJump)
         {
             if (transform.position.y < yValue + 0.0001f)
             {
                 momentum        = JumpPower;
                 randomCon.clips = jumpClips;
                 randomCon.PlaySound();
                 Anim.gameObject.GetComponent <Animator>().enabled = false;
                 SpriteRen.sprite = DinoStill;
             }
             else
             {
                 if (!releasedSpace && transform.position.y < yValue + 2)
                 {
                     momentum += FloatPower;
                 }
             }
         }
         else if (Input.GetKey("down"))
         {
             momentum = -15f;
         }
         if (Input.GetKey("down") && transform.position.y > 0.0001f)
         {
             canJump = false;
         }
         else
         {
             if (!Input.GetKey(Jump[0]) && !Input.GetKey(Jump[1]))
             {
                 canJump = true;
             }
             if (Input.GetKey("down"))
             {
                 //Anim.Play("DinoDuck");
                 //crouching
                 Col.offset = new Vector2(Col.offset.x, -0.7f);
                 Col.size   = new Vector2(Col.size.x, 0.5f);
             }
             else
             {
                 //Anim.Play("Player");
                 //not crouching
                 Col.offset = new Vector2(Col.offset.x, 0f);
                 Col.size   = new Vector2(Col.size.x, 1.5f);
             }
         }
         transform.position = transform.position + new Vector3(0, momentum * Time.deltaTime);
         if (transform.position.y > yValue + 0.0001f)
         {
             if (!(Input.GetKey(Jump[0]) || Input.GetKey(Jump[1])))
             {
                 releasedSpace = true;
             }
             momentum -= Gravity;
         }
         else
         {
             Anim.gameObject.GetComponent <Animator>().enabled = true;
             releasedSpace      = false;
             momentum           = 0;
             transform.position = new Vector3(transform.position.x, yValue);
         }
     }
 }
コード例 #7
0
    // Update is called once per frame
    void Update()
    {
        //move horizontally at an increasing speed (should match camera)
        newXPos            = transform.position;
        newXPos.x         += currHorizontalSpeed;
        transform.position = newXPos;
        if (currHorizontalSpeed != 0 && currHorizontalSpeed <= maxHorizontalSpeed)
        {
            //TODO: += or *=
            currHorizontalSpeed += speedMultiplier * Time.deltaTime;
        }
        groundPosition = GameObject.FindWithTag(groundToFind).transform.position.y;

        //vertical movement uses up/down arrows and space bar as input
        isJump = Input.GetAxis("Jump") > 0;
        if (isJump)
        {
            currJumpHeight = Input.GetAxis("Jump") * maxJumpHeight;
        }
        isDown = Input.GetAxis("Jump") < 0;

        //cooldown after jump
        if (currJumpCooldown > 0.0f)
        {
            currJumpCooldown -= Time.fixedDeltaTime;
        }

        //player can jump if they are on the ground & finished with cooldown
        if (isGrounded && isJump && currJumpCooldown <= 0.0f)
        {
            isGrounded = false;
            gameObject.GetComponent <Rigidbody2D>().velocity = new Vector3(0, currJumpHeight * jumpSpeed, 0);
            randomC.clips = jumpClips;
            randomC.PlaySound(false);
        }
        //at peak, start falling
        if (!isGrounded && gameObject.transform.position.y - OriginalPosition.y >= currJumpHeight * jumpMultiplier)
        {
            gameObject.GetComponent <Rigidbody2D>().velocity = new Vector3(0, currJumpHeight * jumpSpeed * -1, 0);
        }
        //fall down twice as fast
        if (!isGrounded && isDown)
        {
            gameObject.GetComponent <Rigidbody2D>().velocity = new Vector3(0, currJumpHeight * jumpSpeed * quickFallMultiplier * -1, 0);
        }

        //player can duck if they are on the ground and press down arrow
        if (isGrounded && isDown)
        {
            isDuck = true;
        }
        else
        {
            isDuck = false;
        }

        //powerup timer
        if (isPoweredUp)
        {
            currPowerupTime += Time.deltaTime;
            if (currPowerupTime >= maxPowerupTime)
            {
                currPowerupTime = 0.0f;
                ResetPowerups();
            }
        }

        SelectAnimation();
    }