Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

enemy gun shoot

Discussion in '2D' started by skywoker, Apr 17, 2018.

  1. skywoker

    skywoker

    Joined:
    Mar 25, 2018
    Posts:
    9
    guys please tell me, there is a 2D shooting script for the gun, it rotates towards the player but shoots only along the x axis. How to make that bullet fly out in that direction into which the gun looks?


    void Update () {

    if (canShoot && player != null && isVisible) {
    canShoot = false;
    Instantiate (bullet, FirePoint.transform.position, transform.rotation);
    StartCoroutine (firePause());
    }
    Vector3 direction = player.transform.position - transform.position;

    transform.right = direction;

    }
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    If your bullet flies in a straight line wherever it's aiming, then you just need to rotate first, then spawn the bullet:
    Code (CSharp):
    1. if(canShoot && player != null && isVisible) {
    2.     canShoot = false;
    3.     Vector3 direction = player.transform.position - transform.position;
    4.     transform.right = direction;
    5.     Instantiate(bullet, FirePoint.transform.position, transform.rotation);
    6.     StartCoroutine(firePause());
    7. }
     
  3. skywoker

    skywoker

    Joined:
    Mar 25, 2018
    Posts:
    9
    Thank you)
     
  4. skywoker

    skywoker

    Joined:
    Mar 25, 2018
    Posts:
    9
    that is bullet script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class bossBullet : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.  
    8.     Rigidbody2D rb;
    9.  
    10.     public float force;
    11.     public  GameObject FirePoint;
    12.     public int damage;
    13.     public float speed;
    14.     bool Flip;
    15.     Animator anim;
    16.     void Start () {
    17.         player = GameObject.FindGameObjectWithTag ("Player");
    18.         rb = GetComponent <Rigidbody2D> ();
    19.  
    20.         Vector3 direction = player.transform.position - transform.position;
    21.         transform.right = direction;
    22.         }
    23.     void OnTriggerEnter2D (Collider2D other) {
    24.  
    25.         if (other.gameObject.tag == "Player") {
    26.             other.gameObject.SendMessage ("MakeDamage", damage, SendMessageOptions.DontRequireReceiver);
    27.             Destroy (gameObject);
    28.         }
    29.         if (other.gameObject.tag == "Enemy") {
    30.             Destroy (gameObject);
    31.         }
    32.  
    33.     }
    34.         void OnBecameInvisible () {
    35.        
    36.         Destroy (gameObject);
    37.     }
    38.  
    39. }
    40.  
    41.