RotX18 / MP_Group4_EscapingReality

VR Escape room game developed in Unity
3 stars 0 forks source link

End Scene Transition #102

Closed RotX18 closed 2 years ago

RotX18 commented 2 years ago

Objective: When the player completes the game, have theem walk into an object that uses OnCollisionEnter(Collision collision) to transition to the end scene

Feature should:

RotX18 commented 2 years ago

Completed end scene transition feature

Code + description

EndSceneTransition.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EndSceneTransition : MonoBehaviour
{
    private void OnCollisionEnter(Collision collision) {
        if(collision.collider.CompareTag("Player")){
            //if the colliding object has the tag of Player, load the end scene
            SceneManager.LoadScene("EndScene");
        }
    }
}

Description:

Additional changes:

RotX18 commented 2 years ago

EndSceneTransition now only transitions to the end scene when all puzzles have been completed

Implemented a foreach loop to check for the number of completed puzzles with the total number of puzzles in the scene.

Code changes + Description

EndSceneTransition

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class EndSceneTransition : MonoBehaviour
{
    private int _completedPuzzles = 0;

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.CompareTag("Player")){
            //if the colliding object has the tag of Player
            foreach(GameObject ele in GameManager.i.puzzles){
                //for every element in the puzzles array
                if (ele.GetComponent<IPuzzle>().Completed){
                    //if the current puzzle is completed
                    _completedPuzzles++;
                }
            }

            if (_completedPuzzles == GameManager.i.puzzles.Length){
                //if the number of completed puzzles = the total number of puzzles, load the end scene
                SceneManager.LoadScene("EndScene");
            }
            else{
                //else, reset the completed count
                _completedPuzzles = 0;
            }
        }
    }
}

Edits:

RotX18 commented 2 years ago

@RabbitKazma please document the fade transition code changes in EndSceneTransition.cs here

RabbitKazma commented 2 years ago

Button handler code for End Scene transition

Code

ButtonHandler.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ButtonHandler : MonoBehaviour
{
    public GameObject pointerOrigin;
    public Animator transition;

    private Ray _pointerRay;
    private RaycastHit _hit;
    private GameObject _hitObj;
    private bool _doRaycast = false;
    private float _timer = 0f;

    private void Update()
    {
        if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger)){
            //if the right index trigger is pressed, set hit obj to null and do raycast
            _hitObj = null;
            _doRaycast = true;
        }
        else {
            _doRaycast = false;
        }

        //hit obj handling
        if (_hitObj != null && _hitObj.GetComponentInChildren<Button>() != null) {
            char[] remove = { 'b', 't', 'n' };
            transition.SetTrigger("Fade");
            _timer += Time.deltaTime;
            if (_timer > 1)
            {
                SceneManager.LoadScene(_hit.collider.name.TrimStart(remove));
            }
        }
    }

    private void FixedUpdate()
    {
        if (_doRaycast)
        {
            //casting from the main camera to the point on screen where the mouse is
            _pointerRay = new Ray(pointerOrigin.transform.position, pointerOrigin.transform.forward);

            if (Physics.Raycast(_pointerRay, out _hit))
            {
                //if the point where the player click contains a gameobject
                _hitObj = _hit.transform.gameObject;
            }
        }
    }

}

Edits:

RotX18 commented 2 years ago

Thanks @RabbitKazma, will close issue now