コード例 #1
0
    private void FixedUpdate()
    {
        //jump state system to give the player control over the jump
        switch (jumpState)
        {
        case JumpState.None:
            if (groundDown.DoRaycast(transform.position) && jumpStartTimer > 0)
            {
                jumpStartTimer = 0;
                jumpState      = JumpState.Holding;
                jumpHoldTimer  = 0;
                velocity.y     = jumpStartSpeed;
            }
            break;

        case JumpState.Holding:
            jumpHoldTimer += Time.deltaTime;
            if (jumpInputDown == false || jumpHoldTimer > jumpMaxHoldPeriod)
            {
                jumpState = JumpState.None;
                //This gets rid of a stutter in mid air. Because of the LeewayPeriod it stuttered
                if (jumpHoldTimer < JumpInputLeewayPeriod)
                {
                    velocity.y = Mathf.Lerp(jumpMinSpeed, jumpStartSpeed, jumpHoldTimer / jumpMaxHoldPeriod);
                }
            }
            break;
        }
        if (jumpState == JumpState.None)
        {
            velocity.y -= gravity * Time.deltaTime;
        }

        float horizInput  = Input.GetAxisRaw("Horizontal");
        int   wantedDir   = GetSign(horizInput);
        int   velocityDir = GetSign(velocity.x);


        Movement(wantedDir, velocityDir);
        //Raycast detections. For each of the directions the gameobject are stored gameobject variables.
        if (moveUp.lastHit != null)
        {
            if (moveUp.lastHit.tag == "Block")
            {
                moveUp.lastHit.SendMessage("Hit");
                source.PlayOneShot(brick, 1);
                source.pitch   = Random.Range(0.9f, 1.1f);
                moveUp.lastHit = null;
            }
        }
        if (moveUp.lastHit != null)
        {
            if (moveUp.lastHit.tag == "CoinBlock")
            {
                moveUp.lastHit.SendMessage("Hit");
                source.PlayOneShot(brick, 1);
                source.pitch   = Random.Range(0.9f, 1.1f);
                coinAmount    += 1;
                moveUp.lastHit = null;
            }
        }

        if (moveDown.lastHit != null)
        {
            if (moveDown.lastHit.tag == "Goomba")
            {
                moveDown.lastHit.SendMessage("Hit");
                source.PlayOneShot(goomba, 1);
                source.pitch     = Random.Range(0.9f, 1.1f);
                moveDown.lastHit = null;
            }
        }


        if (moveRight.lastHit != null)
        {
            if (moveRight.lastHit.tag == "Goomba")
            {
                if (invincible > 0)
                {
                    moveRight.lastHit.SendMessage("Hit");
                    source.PlayOneShot(goomba, 1);
                    source.pitch = Random.Range(0.9f, 1.1f);
                }
                else
                {
                    sceneloader.SceneLoader(2);
                }
                moveRight.lastHit = null;
            }
        }
        if (moveLeft.lastHit != null)
        {
            if (moveLeft.lastHit.tag == "Goomba")
            {
                if (invincible > 0)
                {
                    moveLeft.lastHit.SendMessage("Hit");
                    source.PlayOneShot(goomba, 1);
                    source.pitch = Random.Range(0.9f, 1.1f);
                }
                else
                {
                    sceneloader.SceneLoader(2);
                }
                moveDown.lastHit = null;
            }
        }
    }