February 15th

My research into Raycasting, what it is and how it works, has allowed me to create the first prototype for our game development. This prototype visualising the main game interaction which is intended to be: using touch controllers to guide an object/character through a virtual environment that is projected onto the walls of the space. The object should follow wherever the cursor is pointing.

Raycast with the Touch Controllers

Set Up: Add the Line Renderer component for both HandAnchors - this allows you to visualise the Raycast

Scripts:

Right Hand Telekinesis Version 1

This script is rewritten to support both touch controllers. The function of the script is to create a Raycast and determine when an object has been hit with them. The script below is for the right hand touch controller.

The writing of this code was supported by a YouTube tutorial by Tyler Halstead. He created this “Telekinesis” script, inspired by the choking mechanics in the game ‘SuperHot’. This script was written with the intention of then using the Raycasts to enlarge and destroy an object, shattering it into pieces. Therefore, the tutorial was only helpful up to a certain point and I had to finish the programming independently...

public class TelekinesesRightHand : MonoBehaviour
{
    public LineRenderer telekinesisLine;

    public float lineWidth = 0.1f;
    public float lineMaxLenght = 1f;

    public bool toggled = false;

    private float HandRight = OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger);

    public bool birdHitRight = false;

    private GameObject bird;

    void Start()
    {
        Vector3[] startLinePositions = new Vector3[2] { Vector3.zero, Vector3.zero };
        telekinesisLine.SetPositions(startLinePositions);
        telekinesisLine.enabled = false;
    }

    void Update()
    {
        HandRight = OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger);

        if (HandRight > 0.9)
        {
            toggled = true;
            telekinesisLine.enabled = true;
        }

        else
        {
            telekinesisLine.enabled = false;
            toggled = false;
            birdHitRight = false;
        }

        if (toggled)
        {
            telekinesis(transform.position, transform.forward, lineMaxLenght);
        }

    }

    private void telekinesis(Vector3 targetPosition, Vector3 direction, float length)
    {
        RaycastHit hit;

        Ray telekinesisOut = new Ray(targetPosition, direction);

        Vector3 endPosition = targetPosition + (length * direction);

        if (Physics.Raycast(telekinesisOut, out hit))
        {
            endPosition = hit.point;

            bird = hit.collider.gameObject;

            if (bird.GetComponent<telekinesisFunction>())
            {
                birdHitRight = true;
                bird.GetComponent<telekinesisFunction>().rightHandRay = true;
                Debug.Log("BirdHitRight Value Is: " + birdHitRight);
            }
            else
            {
                birdHitRight = false;
                Debug.Log("birdHitRight Value Is: " + birdHitRight);
            }

        }

        else if (birdHitRight)
        {
            birdHitRight = false;
            Debug.Log("BirdHitRight Valuse Is " + birdHitRight);
        }

        telekinesisLine.SetPosition(0, targetPosition);
        telekinesisLine.SetPosition(1, endPosition);
    }

}

Right Hand Telekinesis Version 2

The script above was incredibly helpful in allowing me to create the raycast function for this interaction prototype. However, it is not able to be used to actually identify and the move an object, meaning that I had to make my own changes for it to work to the design.

public class TelekinesesRightHand : MonoBehaviour
{
    public LineRenderer telekinesisLine;

    public float lineWidth = 0.01f;
    public float lineMaxLength = 1.5f;

    public bool toggled = false;

    private float HandRight = OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger);

    public bool birdHitRight = false;

    public GameObject bird;

    void Start()
    {
        Vector3[] startLinePositions = new Vector3[2] { Vector3.zero, Vector3.zero };
        telekinesisLine.SetPositions(startLinePositions);
        telekinesisLine.enabled = false;
    }

    void Update()
    {
        HandRight = OVRInput.Get(OVRInput.Axis1D.SecondaryHandTrigger);

        if (HandRight > 0.9)
        {
            toggled = true;
            telekinesisLine.enabled = true;
        }

        else
        {
            telekinesisLine.enabled = false;
            toggled = false;
            
        }

        if (toggled)
        {
            Telekinesis(transform.position, transform.forward, lineMaxLength);
        }

        if (birdHitRight == true)
        {
            Followon(transform.position, transform.forward, lineMaxLength);
        }

    }

    private void Telekinesis(Vector3 targetPosition, Vector3 direction, float length)
    {
        RaycastHit hit;

        Ray telekinesisOut = new Ray(targetPosition, direction);

        Vector3 endPosition = targetPosition + (length * direction);

        if (Physics.Raycast(telekinesisOut, out hit))
        {
            endPosition = hit.point;

            bird = hit.collider.gameObject;

            if (bird.GetComponent<telekinesisFunction>())
            {
                birdHitRight = true;
                bird.GetComponent<telekinesisFunction>().rightHandRay = true;
                Debug.Log("BirdHitRight Value Is: " + birdHitRight);
            }

            // bird.layer = 2;

        }

        telekinesisLine.SetPosition(0, targetPosition);
        telekinesisLine.SetPosition(1, endPosition);
    }

    private void Followon(Vector3 targetPosition2, Vector3 direction2, float length2)
    {
        RaycastHit hit2;

        Ray telekinesisOut2 = new Ray(targetPosition2, direction2);

        Vector3 endPosition2 = targetPosition2 + (length2 * direction2);

        if (Physics.Raycast(telekinesisOut2, out hit2))
        {
            bird.GetComponent<telekinesisFunction>().follow = hit2.point;

        }

        // else
        //{
            // bird.GetComponent<telekinesisFunction>().follow = endPosition2;
        //}

    }

}