コード例 #1
0
ファイル: FireWeaponScript.cs プロジェクト: jclyne/unity
    //--------------------------------
    // 3 - Shooting from another script
    //--------------------------------

    /// <summary>
    /// Create a new projectile if possible
    /// </summary>
    public void Attack(bool isEnemy, float direction)
    {
        if (CanAttack)
        {
            shootCooldown = shootingRate;

            // Create a new shot
            var shotTransform = Instantiate(shotPrefab) as Transform;

            // Assign position
            shotTransform.position = transform.position;

            // The is enemy property
            ShotScript shot = shotTransform.gameObject.GetComponent <ShotScript>();
            if (shot != null)
            {
                shot.isEnemyShot = isEnemy;
            }

            // Make the weapon shot always towards it
            ShotMoveScript move = shotTransform.gameObject.GetComponent <ShotMoveScript>();
            if (move != null)
            {
                move.direction    = this.transform.right;              // towards in 2D space is the right of the sprite
                move.direction.x *= direction;
            }
        }
    }
コード例 #2
0
    public void Attack(Vector3 attackPosition)
    {
        if (CanAttack)
        {
            shootCooldown = shootingRate;
            var shotTransform = Instantiate(shotPrefab) as Transform;
            shotTransform.position = transform.position;

            ShotMoveScript move = shotTransform.gameObject.GetComponent <ShotMoveScript>();
            if (move != null)
            {
                move.attackPosition = attackPosition;
                var heading   = attackPosition - shotTransform.position;
                var distance  = heading.magnitude;
                var direction = heading / distance;
                move.direction = direction.normalized;
                move.speed     = new Vector2(speed, speed);
            }
        }
    }