Beispiel #1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1") && !SaveState.Instance.playerDead && !SaveState.Instance.activeMenu)
        {
            if (launched && orbEnabled)       // When it's launched, we will teleport to it on left click
            {
                //audio bit
                audioSource.clip = orbteleport;
                audioSource.Play();

                //teleport bit
                transform.position = orb.position;
                OrbToPlayer();
                fade.StartFade(new Color(1.0f, 0.92f, 0.016f, 0.25f), fadeTime / 2);
                StartCoroutine(FadeOut(fadeTime / 2));
            }
            else if (!launched && orbHasClearSpace && canFire)                                                                    // If not launched and other conditions are satisfied, we shoot the orb
            {
                orb.transform.position = player.transform.position + player.transform.TransformDirection(Vector3.forward * 1.5f); // remove this line to not have ball move to centre of screen when shooting
                orb.transform.parent   = null;

                Vector3 target        = player.transform.forward * speed * maxDistance;
                Vector3 forwardVector = (target - orb.transform.localPosition).normalized;
                orb.AddForce(forwardVector * speed);
                lastPos  = orb.transform.position;
                launched = true;
                canFire  = false;
                orbScript.OnLaunched();
                orbInd.Activate(orbInd.gameObject);
            }
            else
            {
                //Play an error sound that player can't shoot
                audioSource.clip = orbfailed;
                audioSource.Play();
            }
        }
        // Calculate distance travelled
        if (launched)
        {
            distanceTravelled += Vector3.Distance(orb.transform.position, lastPos);
            lastPos            = orb.transform.position;
        }
        // Force orb to attach to player
        if (!launched)
        {
            orb.transform.localPosition = new Vector3(player.transform.localPosition.x + 0.4f, player.transform.localPosition.y - 0.85f, player.transform.localPosition.z + 0.6f);
        }
        // When orb reaches max distance, return it to the player.
        if (distanceTravelled >= maxDistance)
        {
            OrbToPlayer();
        }
        // Change colour of orb as it nears the end of its range (2/3 of the way)
        if (distanceTravelled >= maxDistance / 2)
        {
            orbP.startColor = Color.Lerp(Color.blue, Color.red, Time.time / 2);
        }
    }