RotX18 / MP_Group4_EscapingReality

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

Start and End Scene #105

Closed RabbitKazma closed 2 years ago

RabbitKazma commented 2 years ago

Completed start scene transition and from end scene back to start scene

Code + description

ButtonHandler.cs

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

public class ButtonHandler : MonoBehaviour
{
    public GameObject pointerOrigin;

    private Ray _pointerRay;
    private RaycastHit _hit;
    private GameObject _hitObj;

    private void Update()
    {

        if (OVRInput.GetDown(OVRInput.RawButton.RIndexTrigger))
        {
            if (_hitObj != null)
            {
                //check if it collides with the gameobject name StartButton
                if (_hitObj.GetComponent<Collider>().name.Equals("StartButton"))
                {
                    //loadScene
                    SceneManager.LoadScene(1);
                    Debug.Log("loadscene");
                }
                else if (_hitObj.GetComponent<Collider>().name.Equals("ReturnButton"))
                {
                    //loadScene
                    SceneManager.LoadScene(0);
                }
            }
            else return;
        }

    }

    private void FixedUpdate()
    {
        //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;
        }
        else
        {
            _hitObj = null;
        }
    }
}

Description:

Notes:

RabbitKazma commented 2 years ago

image image

RabbitKazma commented 2 years ago

image image

RotX18 commented 2 years ago

Button handler code refinement

Rewrote code in ButtonHandler.cs to be non-hardcoded

Code Changes + Description

ButtonHandler.cs

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

public class ButtonHandler : MonoBehaviour
{
    public GameObject pointerOrigin;

    private Ray _pointerRay;
    private RaycastHit _hit;
    private GameObject _hitObj;
    private bool _doRaycast = false;

    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' };
            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;
            }
        }
    }
}

Description: Buttons in the scene that change scenes should be named btnSCENENAME (e.g btnMyScene)

Button handling complete

RotX18 commented 2 years ago

@RabbitKazma please document the ButtonHandler.cs changes (code changes on fade transition) here

RotX18 commented 2 years ago

End scene now contains a text field for completion time

The scene now show the player's completion time

Code + description

EndSceneTimer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class EndSceneTimer : MonoBehaviour
{
    public TextMeshProUGUI txtTimer;

    private void Start() {
        txtTimer.text = $"Completed in {LessThanTen(PlayerPrefs.GetFloat("minutes"))}min {LessThanTen(Mathf.Round(PlayerPrefs.GetFloat("seconds")))}s";
        PlayerPrefs.DeleteAll();
    }

    private string LessThanTen(float f) {
        if(f < 10) {
            return $"0{f}";
        }
        else {
            return f.ToString();
        }
    }
}

Description: Script makes use of PlayerPrefs to keep track of the player's completion time

RabbitKazma commented 2 years ago

Button handler code refinement

Rewrote code in ButtonHandler.cs to be non-hardcoded

Code Changes + Description

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;
            }
        }
    }

}

Description:

RotX18 commented 2 years ago

Thanks @RabbitKazma, will close issue now