Example #1
0
    // Update is called once per frame
    void Update()
    {
        RaycastHit hit;
        Ray        ray   = new Ray(f.transform.position, f.transform.forward);
        bool       isHit = Physics.Raycast(ray, out hit, Mathf.Infinity);

        if (hit.point != Vector3.zero) // Vector3 is non-nullable; comparing to null is always false
        {
            transform.position = hit.point;
        }
        else
        {
            transform.position = ray.GetPoint(3.0f);
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            ShootableTargetLeft  healthLeft  = objectL.GetComponent <ShootableTargetLeft>();
            ShootableTargetRight healthRight = objectR.GetComponent <ShootableTargetRight>();
            if (f.transform.forward.x < 0)
            {
                startTimeL = Time.time;
                healthLeft.Damage(countTime);
                if (isHit)
                {
                    successRate.Add(1);
                    Debug.Log("LEFT Hit successfully");
                }
                else
                {
                    successRate.Add(0);
                    Debug.Log("Left failed");
                }
            }
            if (f.transform.forward.x > 0)
            {
                healthRight.Damage(countTime);
                startTimeR = Time.time;
                if (isHit)
                {
                    Debug.Log("Right Hit successfully");
                    successRate.Add(1);
                }
                else
                {
                    successRate.Add(0);
                    Debug.Log("Right failed");
                }
            }

            durationTime = System.Math.Abs(startTimeL - startTimeR);
            selectionTime.Add(durationTime);
            Debug.Log(startTimeL + "-" + startTimeR + "=" + durationTime);
        }
    }
Example #2
0
    void Update()
    {
        // Check if the player has pressed the fire button and if enough time has elapsed since they last fired
        if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
        {
            // Update the time when our player can fire next
            nextFire = Time.time + fireRate;

            // Start our ShotEffect coroutine to turn our laser line on and off
            StartCoroutine(ShotEffect());

            // Create a vector at the center of our camera's viewport
            Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));

            // Declare a raycast hit to store information about what our raycast has hit
            RaycastHit hit;

            // Set the start position for our visual effect for our laser to the position of gunEnd
            laserLine.SetPosition(0, gunEnd.position);

            // Check if our raycast has hit anything
            if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
            {
                // Set the end position for our laser line
                laserLine.SetPosition(1, hit.point);

                // Get a reference to a health script attached to the collider we hit
                ShootableTargetLeft  healthLeft  = hit.collider.GetComponent <ShootableTargetLeft>();
                ShootableTargetRight healthRight = hit.collider.GetComponent <ShootableTargetRight>();


                // If there was a health script attached for left target
                if (healthLeft != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    healthLeft.Damage(gunDamage);
                }

                // If there was a health script attached for right target
                if (healthRight != null)
                {
                    // Call the damage function of that script, passing in our gunDamage variable
                    healthRight.Damage(gunDamage);
                }
                //if (target != null)
                //{
                //    target.Damage(gunDamage);

                //}

                // Check if the object we hit has a rigidbody attached
                if (hit.rigidbody != null)
                {
                    // Add force to the rigidbody we hit, in the direction from which it was hit
                    hit.rigidbody.AddForce(-hit.normal * hitForce);
                }
            }
            else
            {
                // If we did not hit anything, set the end of the line to a position directly in front of the camera at the distance of weaponRange
                laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
            }
        }
    }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        //Using main camera or the information of main as here is FoveInterface
        //Vector3 headPosition = UnityEngine.FoveInterfaceBase.GetHMDPosition();
        //Quaternion headRotation = UnityEngine.FoveInterfaceBase.GetHMDRotation();

        RaycastHit hit;
        Ray        ray   = new Ray(f.transform.position, f.transform.forward);
        bool       isHit = Physics.Raycast(ray, out hit, Mathf.Infinity);

        //   f.EyeRays rays = FoveInterface.GetEyeRays();

        // TODO: calculate the convergence point in FoveInterface

        // Just hack in to use the left eye for now...



        if (Input.GetKeyDown(KeyCode.Space))
        {
            ShootableTargetLeft  healthLeft  = objectL.GetComponent <ShootableTargetLeft>();
            ShootableTargetRight healthRight = objectR.GetComponent <ShootableTargetRight>();
            if (f.transform.forward.x < 0)
            {
                startTimeL = Time.time;
                healthLeft.Damage(countTime);
                if (isHit)
                {
                    successRate.Add(1);
                    Debug.Log("LEFT Hit successfully");
                }
                else
                {
                    successRate.Add(0);
                    Debug.Log("Left failed");
                }
            }
            if (f.transform.forward.x > 0)
            {
                healthRight.Damage(countTime);
                startTimeR = Time.time;
                if (isHit)
                {
                    Debug.Log("Right Hit successfully");
                    successRate.Add(1);
                }
                else
                {
                    successRate.Add(0);
                    Debug.Log("Right failed");
                }
            }

            durationTime = System.Math.Abs(startTimeL - startTimeR);
            selectionTime.Add(durationTime);
            Debug.Log(startTimeL + "-" + startTimeR + "=" + durationTime);
        }



        //if (isHit)
        //{
        //    ShootableTargetLeft healthLeft = hit.collider.GetComponent<ShootableTargetLeft>();
        //    ShootableTargetRight healthRight = hit.collider.GetComponent<ShootableTargetRight>();


        //    // If there was a health script attached for left target
        //    if (healthLeft != null && Input.GetKeyDown(KeyCode.Space))
        //    {
        //        // Call the damage function of that script, passing in our gunDamage variable
        //        startTimeL = Time.time;
        //        healthLeft.Damage(countTime);
        //        Debug.Log(f.transform.forward.ToString());
        //    }

        //    // If there was a health script attached for right target
        //    if (healthRight != null && Input.GetKeyDown(KeyCode.Space))
        //    {
        //        // Call the damage function of that script, passing in our gunDamage variable
        //        startTimeR = Time.time;
        //        healthRight.Damage(countTime);
        //        Debug.Log(f.transform.forward.ToString());
        //    }
        //    durationTime = System.Math.Abs(startTimeL - startTimeR);
        //    selectionTime.Add(durationTime);
        //    Debug.Log(startTimeL + "-"+ startTimeR + "=" + durationTime);
        //    if (hit.rigidbody != null)
        //    {
        //        // Add force to the rigidbody we hit, in the direction from which it was hit
        //        hit.rigidbody.AddForce(-hit.normal * hitForce);
        //    }

        //}
    }