In my previous post documenting this portotyping process (Unity: Raycast Prototype Versions 1-12), I demonstrated the problems that I was having with the script. The issue that I was struggling to overcome was that the object bouncing between the endpoint of the raycast and the players hand. After further problem solving, I still could not overcome this so I enlisted some help from a friend who has more experience writing in C# and working with VR development. We went over the script together and decded that it may be easier to start from scartch and he could support me as I write it.

New Script:

public class NewTelekinesis : MonoBehaviour
{
    private LineRenderer laser;

    RaycastHit hit = new RaycastHit();

    bool telekinesisOn;

    public GameObject bird;

    private bool HandRightButton;

    private bool birdhit;

    void Start()
    {
        laser = gameObject.GetComponent<LineRenderer>();
    }

    void Update()
    {
        
        if (OVRInput.Get(OVRInput.Button.Two))
        {
            if (telekinesisOn == true)
            {
                telekinesisOn = false;
                bird.layer = LayerMask.NameToLayer("Default");
                birdhit = false;
            }

            else
            {
                telekinesisOn = true;
            }
        }

        if (telekinesisOn == true)
        {
            LaserPointer(transform.position, transform.forward, 6f);
            laser.enabled = true;
        }

        if (telekinesisOn == false)
        {
            laser.enabled = false;
        }

    }

    void LaserPointer(Vector3 TargetPosition, Vector3 direction, float length)
    {
        Ray ray = new Ray(TargetPosition, direction);
        Vector3 endposition = TargetPosition + (direction * length);

        if (Physics.Raycast(ray, out hit))
        {
            // add hitting the bird here
            if (hit.collider.tag == "bird")
            {
                bird.layer = LayerMask.NameToLayer("Ignore Raycast");
                birdhit = true;
            }

            if (birdhit == true)
            {
                if (bird.layer == LayerMask.NameToLayer("Ignore Raycast"))
                {
                    bird.transform.position = hit.point;                
                }
            }
        }

        laser.SetPosition(0, TargetPosition);
        laser.SetPosition(1, endposition);

    }

Result:

462A860D-F391-414C-BDF1-768B837555A0.mov

In this version, I have set-up a simple mockup of the space to the correct dimensions that we have planned relative to the size of the plauer.

As you can see in this video, the object follows the raycast and is no longer bouncing back and forth between the hand and target position. Therfore this script was a much greater success.

Next Step: Restrict the objects movement to the walls of the space as this is where the interactions will take place.

Additions to the code to achieve this:

// set up a bool

private bool wallhit;

// inside the update function:

if (hit.collider.tag == "wall")
            {
                wallhit = true;  
            }

                else
                {
                    wallhit = false;
                }
// And add the wall hit requirement here:

if (birdhit == true)
            {
                if (bird.layer == LayerMask.NameToLayer("Ignore Raycast"))
                {
                    if (wallhit == true)
                    {
                        bird.transform.position = hit.point;
                    }                  
                }
            }

Update:

46BAEF0E-67B4-4E7C-8E9D-D2F94CC25103.mov

In this video you can now see the raycast functioning as expected! The interaction with object takes place only on the walls. Therefore if we want the player to use touch controllers to guide object or features around the space, they can.

If we are to go ahead and use gesture control as we discussed this week, this prototype will still be useful to demo our interaction ideas and share to gather feedback from play-testers.