예제 #1
0
    private void DoRespawn()
    {
        SetState(PlayerState.Idle);

        if (gameHUD)
        {
            gameHUD.DoRespawn();
        }

        gravityAlterations.Clear();
        gravityContacts.Clear();
        gravity     = Physics.gravity;
        gravityLerp = gravityDir;

        Time.timeScale = 1.0f;

        GetComponent <Rigidbody>().velocity = Vector3.zero;

        // We have a checkpoint?
        foreach (GameObject obj in GameObject.FindGameObjectsWithTag("Checkpoint"))
        {
            CheckpointTrigger checkpoint = obj.GetComponent <CheckpointTrigger>();
            if (checkpoint && checkpoint.activated)
            {
                // Restore every collectable active state to the states stored in the checkpoint.
                foreach (GameObject collectable in Collectable.AllCollectables)
                {
                    if (collectable)
                    {
                        collectable.SetActive(true);
                    }
                }
                foreach (GameObject collectable in checkpoint.collectables)
                {
                    if (collectable)
                    {
                        collectable.SetActive(false);
                    }
                }

                // Go to the checkpoint position and rotation.
                transform.position = obj.transform.position;
                transform.rotation = obj.transform.rotation;

                if (playerCamera)
                {
                    playerCamera.Reset();
                }

                if (respawnSound)
                {
                    GetComponent <AudioSource>().PlayOneShot(respawnSound);
                }

                return;
            }
        }

        // Otherwise just start the entire level over again, including the timer.
        GameObject respawnobj = GameObject.FindGameObjectWithTag("Respawn");

        if (respawnobj)
        {
            transform.position = respawnobj.transform.position;
            transform.rotation = respawnobj.transform.rotation;
        }
        else
        {
            transform.position = Vector3.zero;
            transform.rotation = Quaternion.identity;
        }

        levelStartTime = 0.0f;

        // Restore all the collectables.
        if (Collectable.AllCollectables != null)
        {
            foreach (GameObject collectable in Collectable.AllCollectables)
            {
                if (collectable)
                {
                    collectable.SetActive(true);
                }
            }
        }
    }