// Start is called before the first frame update
 void Start()
 {
     if (photonView.isMine)
     {
         playerHealth = GetComponentInParent <PhotonHealth>();
         audioSource  = GetComponent <AudioSource>();
     }
 }
Beispiel #2
0
    void Fire()
    {
        RaycastHit hit = new RaycastHit();

        Ray ray = new Ray(cam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)), cam.transform.TransformDirection(Vector3.forward));

        //Test Hit
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log(hit.collider.gameObject.tag);
        }

        if (Physics.Raycast(ray, out hit, weaponRange))
        {
            if (hit.collider.gameObject.tag == "Player")
            {
                hitCount++;

                GameObject impactObj = Instantiate(impactPlayer, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactObj, 2f);

                PhotonHealth health = hit.collider.gameObject.GetComponent <PhotonHealth>();

                int dmg = Random.Range(5, 10);

                if (health != null)
                {
                    if (health.currentHealth - dmg <= 0)
                    {
                        GetComponent <RoundManager>().addPoint("player");
                        GetComponent <RoundManager>().addScore("player");
                        //GetComponent<PhotonHealth>().Respawn();
                        GetComponent <PhotonHealth>().currentHealth = 100;
                        health.currentHealth = 100;
                        GetComponent <PhotonView>().RPC("Respawn", PhotonTargets.AllBuffered);
                        hit.transform.GetComponent <PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, dmg);
                    }
                    else
                    {
                        hit.transform.GetComponent <PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, dmg);
                    }
                }

                StartCoroutine(HoldTime(cHit.gameObject, 0.5f));
            }
            else if (hit.collider.gameObject.tag == "Stone")
            {
                GameObject impactObj = Instantiate(impactStone, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactObj, 2f);
            }
            else if (hit.collider.gameObject.tag == "Wood")
            {
                GameObject impactObj = Instantiate(impactWood, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactObj, 2f);
            }
            else if (hit.collider.gameObject.tag == "Metal")
            {
                GameObject impactObj = Instantiate(impactMetal, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactObj, 2f);
            }
            else if (hit.collider.gameObject.tag == "Sand")
            {
                GameObject impactObj = Instantiate(impactSand, hit.point, Quaternion.LookRotation(hit.normal));
                Destroy(impactObj, 2f);
            }
        }
    }