Newer
Older
CGTrack / Assets / Scripts / Bausatz / VRShoot.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Bausatz
{
    public class VRShoot : MonoBehaviour
    {
        public Transform anchor;
        public GameObject toInstantiate; // the game object to instantiate
       
        private float _nextActionTime;
        public float period = 0.1f;
        public float launchVelocity = 700f;
        public int score;

        public bool change;
        private bool _change;
        public Text scoreText;

        private void InstantiateObject()
        {
            var projectile = Instantiate(toInstantiate, anchor.position, anchor.rotation);
            
            projectile.GetComponent<Rigidbody>().AddRelativeForce(new Vector3 
                (0, 0,launchVelocity));

            projectile.GetComponent<Bullet>().SetShoot(this);
            
            Destroy(projectile, 5f);
        }

        public void Hit()
        {
            score++;
            UpdateGUI();
        }

        private void UpdateGUI()
        {
            scoreText.text = $"Score: {score}";
        }

        private void Start()
        {
            UpdateGUI();
        }

        private void OnDrawGizmos()
        {
            
            Gizmos.color = Color.green;
            Gizmos.DrawLine(anchor.position, anchor.position + 3 * anchor.forward);
        }

        // Update is called once per frame
        private void Update()
        {

            if (change != _change)
            {
                _change = change;
                InstantiateObject();
            }
            
            if (!(Time.time > _nextActionTime)) return;
            _nextActionTime += period;
                
            if (OVRInput.Get(OVRInput.Axis1D.SecondaryIndexTrigger) > 0.8f)
            {
                InstantiateObject();
            }
        }
    }
}