Attack() public method

Create a new projectile if possible
public Attack ( bool isEnemy ) : void
isEnemy bool
return void
示例#1
0
    // Update is called once per frame
    void OnGUI()
    {
        bool shoot = Input.GetKey(KeyCode.J);

        if (shoot)
        {
            // shootsound.Play();
            WeaponScript weapon = GetComponentInChildren <WeaponScript>();
            if (weapon != null)
            {
                if (Input.GetKey(KeyCode.D))
                {
                    weapon.Attack(new Vector2(1, 0));
                }
                else if (Input.GetKey(KeyCode.A))
                {
                    weapon.Attack(new Vector2(-1, 0));
                }

                else if (dir == dirfront)
                {
                    weapon.Attack(new Vector2(1, 0));
                }
                else
                {
                    weapon.Attack(new Vector2(-1, 0));
                }
            }
        }
    }
示例#2
0
    void Update()
    {
        // 3 - Retrieve axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Movement per direction
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);

        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");


        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null && weapon.CanAttack)
            {
                weapon.Attack(false);
            }
        }
    }
示例#3
0
    // Update is called once per frame
    void Update()
    {
        //用于获取X轴的输入
        float inputX = Input.GetAxis("Horizontal");
        //用于获取Y轴的输入
        float inputY = Input.GetAxis("Vertical");

        //角色速度的计算公式(输入方向*速度大小)
        movement = new Vector3(inputX * speed.x, inputY * speed.y, 0);

        //点击鼠标发射子弹
        bool shoot = Input.GetButtonDown("Fire1");

        if (shoot)
        {
            //获取武器脚本,并且检测角色是否带有武器
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                //每次攻击完成后,把武器关闭并且播放攻击声音
                weapon.Attack(false);
                SoundEfectsHelper.Instance.MakePlayerShotSound();
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        //Here we get the information about what the player done.
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        //We affect the movement in function of speed.
        movement = new Vector2(speed.x * inputX, speed.y * inputY);

        //Here we get the action of shooting by pressing space button.
        bool shoot = Input.GetKeyDown(KeyCode.Space);

        //shoot |= Input.GetKeyDown (KeyCode.Space);
        //If the player press space.
        if (shoot)
        {
            //We get the weapon
            WeaponScript weapon = GetComponent <WeaponScript> ();
            if (weapon != null)
            {
                //False because player is not an ennemy.
                weapon.Attack(false);
            }
        }
    }
    void Update()
    {
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);

        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null && weapon.CanAttack)
            {
                weapon.Attack(false);
                SoundEffectsHelper.Instance.MakePlayerShotSound();
            }
        }

        var dist         = (transform.position - Camera.main.transform.position).z;
        var leftBorder   = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).x;
        var rightBorder  = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, dist)).x;
        var topBorder    = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).y;
        var bottomBorder = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, dist)).y;

        transform.position = new Vector3(
            Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
            Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
            transform.position.z
            );
    }
示例#6
0
    // Attack the player if player spotted and in attack range
    private void AttackPlayer()
    {
        //Make sure enemy doesn't move
        agent.SetDestination(transform.position);

        // Make AI face the player when attacking
        if (isFlying)
        {
            flyingBody.transform.LookAt(player);
            if (!playerInYRange)
            {
                FlyTowards(player.position);
            }
        }
        else
        {
            transform.LookAt(player);
        }

        // Check if AI has already attacked the player
        if (!alreadyAttacked)
        {
            weapon.Attack();

            //According to Unity Docs, Coroutines work better than Invoke, so using Coroutine instead
            // alreadyAttacked = true;
            // Invoke(nameof(ResetAttack), timeBetweenAttacks);
            StartCoroutine(ResetAttack());
        }
    }
示例#7
0
 //check if weapon is off cooldown and can attack
 void Update()
 {
     if (weapon != null && weapon.CanAttack)
     {
         weapon.Attack(true);
     }
 }
示例#8
0
    // Update is called once per frame
    void Update()
    {
        heartsUI.sprite = heartsSprites[this.health];

        // Get axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // Check if object move and flip sprite if it moves to left
        if (inputX != 0)
        {
            this.gameObject.GetComponent <SpriteRenderer>().flipX = movementLeft = inputX < 0;
        }

        // Movement per direction
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);

        // Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }
    }
示例#9
0
    void Update()
    {
        float inputX = Input.GetAxis("Horizontal");
        bool  shoot  = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (Input.GetButtonDown("Jump") && rigidbodyComponent.velocity.y == 0)
        {
            rigidbodyComponent.velocity = Vector2.up * jumpVelocity;
        }
        else if (rigidbodyComponent.velocity.y > 0 && !Input.GetButton("Jump"))
        {
            rigidbodyComponent.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
        }
        if (rigidbodyComponent.velocity.y < 0)
        {
            rigidbodyComponent.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }

        movement = new Vector2(
            speed.x * inputX,
            rigidbodyComponent.velocity.y
            );

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                weapon.Attack(false);
                FindObjectOfType <AudioManager>().Play("PlayerShot");
            }
        }
    }
示例#10
0
    void Update()
    {
        // Touchscreen input
        // Look for all fingers
        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch touch = Input.GetTouch(i);

            // -- Drag
            // ------------------------------------------------
            if (touch.phase == TouchPhase.Moved)
            {
                movement = new Vector2(0, touch.deltaPosition.y * 4);
            }
        }

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }
    }
示例#11
0
    // Update is called once per frame
    void Update()
    {
        // извлечение координат корабля
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // задаем направление корабля
        movement = new Vector2(speed.x * inputX, speed.y * inputY);

        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                weapon.Attack(false);
                SoundsEffectHelper.instance.makePlayerShotSound();
            }
        }

        // игрок не должен выходить за рамки игры
        var dist         = (transform.position - Camera.main.transform.position).z;
        var leftBorder   = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).x;
        var rightBorder  = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, dist)).x;
        var topBorder    = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, dist)).y;
        var bottomBorder = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, dist)).y;

        transform.position = new Vector3(Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
                                         Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
                                         transform.position.z);
    }
示例#12
0
    private void Attack()
    {
        if (myattackbutton != null)
        {
            if (myattackbutton.Pressed == true)
            {
                WeaponScript weapon = GetComponent <WeaponScript>();
                if (weapon != null)
                {
                    weapon.Attack();
                }
            }
        }

        //var horiz = Input.GetAxis("Horizontal");
        //var vert = Input.GetAxis("Vertical");
        if (weaponjoystick != null)
        {
            float horiz = weaponjoystick.Horizontal;
            float vert  = weaponjoystick.Vertical;

            if (horiz > this.positiveDeadZone | horiz <this.negativeDeadZone | vert> this.positiveDeadZone | vert < this.negativeDeadZone)
            {
                Weapon2 weapon2 = GetComponent <Weapon2>();
                if (weapon2 != null)
                {
                    weapon2.Attack(horiz, vert);
                }
            }
        }
    }
示例#13
0
    void Update()
    {
        // ...
        if (shootCooldown > 0)
        {
            shootCooldown -= Time.deltaTime;
        }
        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }

        // ...
    }
示例#14
0
    void Update()
    {
        // Retrieve axis information
        directionInput += Input.GetAxis("Horizontal");
        thrustInput    += Input.GetAxis("Vertical");

        if (thrustInput < 0)
        {
            thrustInput = 0; // clamp out backwards movement
        }

        // Reflection
        hit = Physics2D.Raycast(myTrans.position, myTrans.up, 0.5f, 1 << LayerMask.NameToLayer("walls"));


        // Shooting

        bool shoot = Input.GetButton("Fire1");

        shoot |= Input.GetButton("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                weapon.Attack(false); // not isEnemy
            }
        }
    }
示例#15
0
    void Update()
    {
        //Set the enemy sprite order equal to our enemy's feet Y position
        enemySprite.sortingOrder = -(int)enemyFeet.position.y;

        if (transform.position.x < playerBody.position.x)
        {
            enemyBody.rotation = Quaternion.Euler(new Vector3(0, 180, 0));
        }
        else if (transform.position.x > playerBody.position.x)
        {
            enemyBody.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
        }

        // Auto-fire
        enemyWeapon = GetComponentInChildren <Transform>();

        if (enemyWeapon.position.y < playerBody.position.y + 0.5f && enemyWeapon.position.y > playerBody.position.y - 0.5f)
        {
            if (weapon != null && weapon.CanAttack)
            {
                weapon.Attack(true);
            }
        }
    }
示例#16
0
    void Update()
    {
        // 3 - Récupérer les informations du clavier/manette
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Calcul du mouvement
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);

        // 5 - Tir
        bool shoot = Input.GetButton("Fire1");

        shoot |= Input.GetButton("Fire2");
        // Astuce pour ceux sous Mac car Ctrl + flèches est utilisé par le système

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false : le joueur n'est pas un ennemi
                weapon.Attack(false);
            }
        }
    }
示例#17
0
    // Update is called once per frame
    void Update()
    {
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        movement = new Vector2(speed.x * inputX, speed.y * inputY);

        // ...

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
                SoundEffectsHelper.Instance.MakePlayerShotSound();
            }
        }

        // ...
    }
 // Update is called once per frame
 void Update()
 {
     if (weapon != null)
     {
         weapon.Attack();
     }
 }
    void Update()
    {
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        if (inputX != 0)
        {
            movement = new Vector2(speed.x * inputX, 0);
        }
        else
        {
            movement = new Vector2(0, speed.y * inputY);
        }
        Rotate(inputX, inputY);                 // Get rotation vector
        transform.eulerAngles = rotationVector; // Apply rotation

        //Debug.Log("inputX: " + inputX);
        //Debug.Log("inputY: " + inputY);

        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                //Debug.Log("Shooting");
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }
    }
    void Update()
    {
        // 3 - Retrieve axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Movement per direction
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }

        // here we check input for the shrink and execute it
        CheckActivateShrink();

        OutsideOfCamera();

        // count the score with a +1
        CommonUtils.IncreaseScore(1);
    }
示例#21
0
 // Update is called once per frame
 void Update()
 {
     //如果之前没有被渲染,且主相机渲染到目标
     if (hasSpawn == false)
     {
         if (renderer.IsVisibleFrom(Camera.main))
         {
             //调用开状态函数,把目标状态全开
             Spawn();
         }
     }
     else
     {
         //如果目标身上带有武器
         if (weapon != null && weapon.CanAttack)
         {
             //把攻击值设置为可以攻击,并且播放攻击声音
             weapon.Attack(true);
             SoundEfectsHelper.Instance.MakeEnemyShotSound();
         }
         //如果子弹离开主相机的渲染范围,则销毁子弹
         if (renderer.IsVisibleFrom(Camera.main) == false)
         {
             Destroy(gameObject);
         }
     }
 }
示例#22
0
    void Update()
    {
        if (restart)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                Application.LoadLevel(Application.loadedLevel);
            }
        }
        if (gameover == true)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                Application.LoadLevel(Application.loadedLevel);
            }
        }

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }
    }
示例#23
0
    void Update()
    {
        // 3 - извлечь информацию оси

        float inputX = Input.GetAxis("Horizontal");

        float inputY = Input.GetAxis("Vertical");

        // 4 - движение в каждом направлении

        movement = new Vector2(

            speed.x * inputX,

            speed.y * inputY);


        bool         shoot  = Input.GetKeyDown(KeyCode.Space);
        WeaponScript weapon = GetComponent <WeaponScript> ();

        if (shoot)
        {
            if (weapon != null)
            {
                weapon.Attack(false);
                SoundEffectsHelper.Instance.MakePlayerShotSound();
            }
        }


        Vector3 pos = transform.position;

        transform.position = new Vector3(Mathf.Clamp(pos.x, leftBorder, rightBorder), Mathf.Clamp(pos.y, bottomBorder, topBorder), pos.z);
    }
示例#24
0
    new void Update()
    {
        if (alive)
        {
            // Call parent update
            base.Update();

            // Look at the mouse direction
            RotateToPos(Camera.main.ScreenToWorldPoint(Input.mousePosition));

            // Attack !
            if (Input.GetMouseButton(0) && attachedWeapon)
            {
                weaponScript.Attack();
            }

            // Throw weapon
            if (Input.GetMouseButtonDown(1) && attachedWeapon)
            {
                ThrowWeapon();
            }

            // Pickup weapon
            if (Input.GetKeyDown(KeyCode.E) && lastCollidedWeapon)
            {
                PickupWeapon(lastCollidedWeapon);
            }
        }
    }
示例#25
0
    void Update()
    {
        var move = new Vector3(Input.GetAxis("Horizontal"), 0, 0);

        transform.position += move * speed * Time.deltaTime;


        // ...

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }

        // ...
    }
示例#26
0
 // OnTriggerEnter2D is called when the Collider2D other enters the trigger (2D physics only)
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.GetComponentInParent <Enemy>() != null)
     {
         ws.Attack(collision.GetComponentInParent <Enemy>(), damage);
     }
 }
示例#27
0
 /// <summary>
 /// Атака основного оружия
 /// </summary>
 public void MainAttack()
 {
     if (mainWeapon != null && mainElements.activ)
     {
         mainWeapon.Attack();
     }
 }
示例#28
0
    // Update is called once per frame
    void Update()
    {
        // 3 - Retrieve axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Movement per direction
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);


        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
            }
        }
    }
示例#29
0
    // Update is called once per frame
    void Update()
    {
        float movementX = Input.GetAxis("Horizontal");
        float movementY = Input.GetAxis("Vertical");

        Vector3 direction = new Vector3(speed.x * movementX, speed.y * movementY, 0);

        //smooth out movement on screen.
        direction *= Time.deltaTime;

        transform.Translate(direction);

        //use weapon

        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");

        //if shoot = true;
        if (shoot)
        {   //call WeaponScript as weapon
            WeaponScript weapon = GetComponent <WeaponScript>();
            //if weapon can't attack
            if (weapon != null)
            {
                //don't attack
                weapon.Attack(false);
            }
        }
    }
示例#30
0
    void Update()
    {
        // 3 - Retrieve axis information
        float inputX = Input.GetAxis("Horizontal");
        float inputY = Input.GetAxis("Vertical");

        // 4 - Movement per direction
        movement = new Vector2(
            speed.x * inputX,
            speed.y * inputY);
        // ...

        // 5 - Shooting
        bool shoot = Input.GetButtonDown("Fire1");

        shoot |= Input.GetButtonDown("Fire2");
        // Careful: For Mac users, ctrl + arrow is a bad idea

        if (shoot)
        {
            WeaponScript weapon = GetComponent <WeaponScript>();
            if (weapon != null)
            {
                // false because the player is not an enemy
                weapon.Attack(false);
                SoundEffectHelper.Instance.MakePlayerShotSound();
            }
        }
        // ...

        // 6 - Make sure we are not outside the camera bounds
        var dist = (transform.position - Camera.main.transform.position).z;

        var leftBorder = Camera.main.ViewportToWorldPoint(
            new Vector3(0, 0, dist)
            ).x;

        var rightBorder = Camera.main.ViewportToWorldPoint(
            new Vector3(1, 0, dist)
            ).x;

        var topBorder = Camera.main.ViewportToWorldPoint(
            new Vector3(0, 0, dist)
            ).y;

        var bottomBorder = Camera.main.ViewportToWorldPoint(
            new Vector3(0, 1, dist)
            ).y;

        transform.position = new Vector3(
            Mathf.Clamp(transform.position.x, leftBorder, rightBorder),
            Mathf.Clamp(transform.position.y, topBorder, bottomBorder),
            transform.position.z
            );

        // End of the update method

        // ...
    }