コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        //Default color
        Color color = Color.white;

        updateModel();
        updateTrackpad();

        //On the first frame of the click, instantiate a projectile at the position of the trackpad click
        if (CC_INPUT.GetButtonDown(Wand.Left, WandButton.TrackpadClick))
        {
            Debug.Log("trackpad just clicked");
            //Color is calculated using the angle of the click
            color            = new Color(Mathf.Cos(angle), Mathf.Cos(angle + (3 * Mathf.PI / 4)), Mathf.Cos(angle - (3 * Mathf.PI / 4)));
            controlledObject = Instantiate(projectile, new Vector3(x, y + transform.position.y, transform.position.z), Quaternion.identity);
            controlledObject.GetComponent <Renderer>().material.color = color;
            controlledObject.name = "obj_" + projectileIncrement;
            projectileIncrement   = (projectileIncrement + 1) % projectileLimit;
            //When the trackpad is held, the projectile follows the position of the trackpad click and its color is updated
            //to match the new angle
        }
        else if (CC_INPUT.GetButtonPress(Wand.Left, WandButton.TrackpadClick) && (controlledObject != null))
        {
            Debug.Log("trackpad being held");
            controlledObject.transform.position = new Vector3(x, y + transform.position.y, transform.position.z);
            color = new Color(Mathf.Cos(angle), Mathf.Cos(angle + (3 * Mathf.PI / 4)), Mathf.Cos(angle - (3 * Mathf.PI / 4)));
            controlledObject.GetComponent <Renderer>().material.color = color;
            //When the trackpad is released, the force is added to the projectile at the angle it was released at
        }
        else if (CC_INPUT.GetButtonUp(Wand.Left, WandButton.TrackpadClick))
        {
            Debug.Log("trackpad just released");
            //A random z-axis force is applied to the projectile
            float force = (float)forceEnum * forceFactor + forceFactor;
            zForce = CC_CANOE.RandomRange(0f, 1f) * force;
            if (CC_CANOE.RandomRange(0, 1) == 0)
            {
                zForce = -zForce;
            }
            //Force is added to the projectile
            if (controlledObject != null)
            {
                controlledObject.GetComponent <Rigidbody>().AddForce(new Vector3(Mathf.Cos(angle) * force, Mathf.Sin(angle) * force, zForce));
            }
            //If an object of the same name exists, destroy it. This enforces the number of projectiles allowed at once.
            if (GameObject.Find("obj_" + projectileIncrement) != null)
            {
                Destroy(GameObject.Find("obj_" + projectileIncrement));
            }
        }

        if (CC_INPUT.GetButtonDown(Wand.Left, WandButton.Trigger))
        {
            forceEnum = (Force)(((int)(forceEnum) + 1) % System.Enum.GetValues(typeof(Force)).Length);
        }

        if (CC_INPUT.GetButtonDown(Wand.Left, WandButton.Grip))
        {
            modelEnum = (Model)(((int)(modelEnum) + 1) % System.Enum.GetValues(typeof(Model)).Length);
            switch (modelEnum)
            {
            case Model.Orb:
                projectile = orbObject;
                break;

            case Model.Cube:
                projectile = cubeObject;
                break;

            default:
                break;
            }
        }

        //Press the left Menu button to delete all existing projectiles
        if (CC_INPUT.GetButtonDown(Wand.Left, WandButton.Menu))
        {
            for (int i = 0; i < projectileLimit; i++)
            {
                if (GameObject.Find("obj_" + i) != null)
                {
                    Destroy(GameObject.Find("obj_" + i));
                    audioSource.Play();
                }
            }
        }

        toggleInfo.text = "Newly spawned projectiles"
                          + "\nwill be " + modelEnum + "s with " + forceEnum + " force";
    }