Esempio n. 1
0
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKey(keyToPress) &&
                Time.time >= timeOfLastSpawn + creationRate)
            {
                Vector2 actualBulletDirection = (relativeToRotation) ? (Vector2)(Quaternion.Euler(0, 0, transform.eulerAngles.z) * shootDirection) : shootDirection;

                GameObject newObject = Instantiate <GameObject>(prefabToSpawn);
                newObject.transform.position    = this.transform.position;
                newObject.transform.eulerAngles = new Vector3(0f, 0f, Utils.Angle(actualBulletDirection));
                newObject.tag = "Bullet";

                // push the created objects, but only if they have a Rigidbody2D
                Rigidbody2D rigidbody2D = newObject.GetComponent <Rigidbody2D>();
                if (rigidbody2D != null)
                {
                    rigidbody2D.AddForce(actualBulletDirection * shootSpeed, ForceMode2D.Impulse);
                }

                // add a Bullet component if the prefab doesn't already have one, and assign the player ID
                BulletAttribute b = newObject.GetComponent <BulletAttribute>();
                if (b == null)
                {
                    b = newObject.AddComponent <BulletAttribute>();
                }
                b.playerId = playerNumber;



                timeOfLastSpawn = Time.time;
            }
        }
Esempio n. 2
0
        // This function gets called everytime this object collides with another trigger
        private void OnTriggerEnter2D(Collider2D collisionData)
        {
            // is the other object a Bullet?
            if (collisionData.gameObject.CompareTag("Bullet"))
            {
                if (userInterface != null)
                {
                    // add one point
                    BulletAttribute b = collisionData.gameObject.GetComponent <BulletAttribute>();
                    if (b != null)
                    {
                        userInterface.AddPoints(b.playerId, pointsWorth);
                    }
                    else
                    {
                        Debug.Log("Use a BulletAttribute on one of the objects involved in the collision if you want one of the players to receive points for destroying the target.");
                    }
                }
                else
                {
                    Debug.Log("There is no UI in the scene, hence points can't be displayed.");
                }

                // then destroy this object
                Destroy(gameObject);
            }
        }