コード例 #1
0
    private void Stab(Vector3 direction)
    {
        // Check if there are any colliders inside a given sphere
        Collider[] colls = Physics.OverlapSphere(transform.position, weapon.range);

        foreach (Collider coll in colls)
        {
            // Is the collider a character?
            if (coll.gameObject != gameObject && (coll.tag == "Player" || coll.tag == "NPC"))
            {
                // calculate angle between character and forward direction
                Vector3 delta = coll.transform.position - transform.position;
                float   angle = Vector3.Angle(direction, delta);

                // is the attack aimed close enough to the character?
                if (angle <= weapon.spread * 0.5f)
                {
                    // Get other character's properties
                    CharacterProperties prop = coll.GetComponent <CharacterProperties> ();

                    // Apply damage to other character
                    prop.DealDamage(weapon.damage);
                    if (pistol.GetComponent <WeaponProperties>().ammo == 0)
                    {
                        CmdSpawn(fxReload);
                    }
                    pistol.GetComponent <WeaponProperties>().ammo = 1;

                    if (properties.target != coll.gameObject)
                    {
                        CmdActivateParticleSystem();
                        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
                        foreach (GameObject player in players)
                        {
                            CharacterProperties playerprop = player.GetComponent <CharacterProperties>();
                            if (playerprop.target == coll.gameObject)
                            {
                                playerprop.target = prop.target;
                                PlayerController pc = player.GetComponent <PlayerController>();
                                pc.RpcSetNote();
                            }
                        }
                    }
                    else
                    {
                        properties.target = prop.target;
                        RpcSetNote();
                    }
                }
            }
        }
    }
コード例 #2
0
    private void Shoot(Vector3 position, Vector3 direction)
    {
        // Does the weapon have ammo left?
        if (weapon.ammo <= 0)
        {
            CmdSpawn(fxGunClick);
            return;
        }
        CmdSpawn(fxGunshot);

        // Decrement ammo amount
        weapon.ammo--;

        // Hit object output from raycasts
        RaycastHit hit;

        // Draw debug ray from the camera to the reticle
        Debug.DrawRay(position, direction.normalized * 100.0f, Color.red, 100.0f);

        // Do raycast from the camera to the reticle
        if (Physics.Raycast(position, direction, out hit, 100.0f))
        {
            // Calculate direction from character to the hit position
            Vector3 dir = hit.point - transform.position;

            // Draw debug ray from the character to the hit position
            Debug.DrawRay(transform.position, dir.normalized * weapon.range, Color.green, 100.0f);

            // Do raycast from the character to the hit position
            if (Physics.Raycast(transform.position, dir, out hit, weapon.range))
            {
                // Do we hit another character?
                if (hit.collider.tag == "Player" || hit.collider.tag == "NPC")
                {
                    // Get other character's properties
                    CharacterProperties prop = hit.collider.GetComponent <CharacterProperties> ();

                    // Apply damage to other character
                    prop.DealDamage(weapon.damage);

                    if (weapon.ammo != 1)
                    {
                        CmdSpawn(fxReload);
                    }

                    weapon.ammo = 1;

                    /*if (!prop.isAlive) {
                     *                          weapon.ammo = 1;
                     *                          if (prop.target)
                     *                                  properties.target = prop.target;
                     *                  }*/
                    if (properties.target != hit.collider.gameObject)
                    {
                        CmdActivateParticleSystem();
                        GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
                        foreach (GameObject player in players)
                        {
                            CharacterProperties playerprop = player.GetComponent <CharacterProperties>();
                            if (playerprop.target == hit.collider.gameObject)
                            {
                                playerprop.target = prop.target;
                                PlayerController pc = player.GetComponent <PlayerController>();
                                pc.RpcSetNote();
                            }
                        }
                    }
                    else
                    {
                        properties.target = prop.target;
                        RpcSetNote();
                    }
                }
            }
        }
    }