Example #1
0
    void Update()
    {
        if (photonView.IsMine && !imDie)
        {
            // move player direction to the cursor
            Vector2 positionOnScreen = Camera.main.WorldToViewportPoint(transform.position);
            Vector2 mouseOnScreen    = (Vector2)Camera.main.ScreenToViewportPoint(Input.mousePosition);
            float   angle            = AngleBetweenTwoPoints(positionOnScreen, mouseOnScreen);
            transform.rotation = Quaternion.Euler(new Vector3(0f, 0f, angle + 90));
            // Debug.Log("sending out ray");
            // RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
            // GameObject target = hit.collider.gameObject;
            PlayerManager plyManager = this.gameObject.GetComponent <PlayerManager>();
            if (Input.GetMouseButton(0) && canShoot && plyManager.energy >= plyManager.defaultShootingEnergyConsuming)
            {
                canShoot           = false;
                plyManager.energy -= plyManager.defaultShootingEnergyConsuming;
                nextShotTimestamp  = Time.time + shootCooldown;
                // if (Time.time >= shootTimeStamp + shootCooldown)
                // {
                // shootTimeStamp = Time.time;
                Vector2  currentMouseVector = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                object[] myCustomInitData   = new object[] { color };

                GameObject bullet          = PhotonNetwork.Instantiate(this.bulletPrefabs[color].name, transform.position, Quaternion.Euler(0, 0, angle + 90), 0, myCustomInitData);
                Vector2    shootDirection  = (Vector2)Vector3.Normalize(currentMouseVector - (Vector2)bullet.transform.position);
                Vector2    shootDirection2 = (Vector2)Vector3.Normalize(currentMouseVector - (Vector2)transform.position);


                bullet.GetComponent <ProjectileController_Network>().parentObject = gameObject;
                bullet.GetComponent <ProjectileController_Network>().moveToTarget(shootDirection);
                // }
            }
            //GameObject manager = GameObject.Find("Game Manager");



            if (Time.time >= nextShotTimestamp)
            {
                canShoot = true;
            }
        }
        if (imDie && photonView.IsMine)
        {
            if (Input.GetKeyDown("space"))
            {
                _cameraWork.StopFollowing();
                watchingPlayer = (watchingPlayer + 1) % Players.Length;
                if (Players[watchingPlayer] == null)
                {
                    watchingPlayer = (watchingPlayer + 1) % Players.Length;
                }
                _cameraWork = Players[watchingPlayer].GetComponent <CameraWork>();
                _cameraWork.OnStartFollowing();
            }
        }
    }