Search Unity

Why does my 2D enemies refuse to shoot to the left?

Discussion in '2D' started by Lotias90, Oct 8, 2019.

  1. Lotias90

    Lotias90

    Joined:
    Nov 11, 2015
    Posts:
    30
    Hello,

    So Im working on this simple 2D sidescrolling shooter. I created a script for the player and him shooting on command which works fine.

    Next Im trying to get my enemies to shoot bullets at intervals, just to add some difficulty.
    I got a script for the enemy shooting and an enemy bullet, it all seems to work fine, except for all the shooting is just flying off to the right?

    I really cant figure out what Im missing or doing wrong.

    The script for my enemy are:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyShooting : MonoBehaviour {
    6.  
    7.     public Transform target; //where we want to shoot(player? mouse?)
    8.     public Transform weaponMuzzle; //The empty game object which will be our weapon muzzle to shoot from
    9.     public GameObject bullet; //Your set-up prefab
    10.     public float fireRate = 3000f; //Fire every 3 seconds
    11.     public float shootingPower = 20f; //force of projection
    12.  
    13.  
    14.     private float shootingTime; //local to store last time we shot so we can make sure its done every 3s
    15.  
    16.     public int health = 100;
    17.     public GameObject deathEffect;
    18.  
    19.     private void Update()
    20.     {
    21.         Fire(); //Constantly fire
    22.     }
    23.  
    24.     private void Fire()
    25.     {
    26.         if (Time.time > shootingTime)
    27.         {
    28.             shootingTime = Time.time + fireRate / 1000; //set the local var. to current time of shooting
    29.             Vector2 myPos = new Vector2(weaponMuzzle.position.x, weaponMuzzle.position.y); //our curr position is where our muzzle points
    30.             GameObject projectile = Instantiate(bullet, myPos, Quaternion.identity); //create our bullet
    31.             Vector2 direction = myPos - (Vector2)target.position; //get the direction to the target
    32.             projectile.GetComponent<Rigidbody2D>().velocity = direction * shootingPower; //shoot the bullet
    33.         }
    34.     }
    35.  
    36.     public void TakeDamage(int damage)
    37.     {
    38.         health -= damage;
    39.  
    40.         if (health <= 0)
    41.         {
    42.             Die();
    43.         }
    44.     }
    45.  
    46.     void Die()
    47.     {
    48.         Instantiate(deathEffect, transform.position, Quaternion.identity);
    49.         Destroy(gameObject);
    50.     }
    51. }
    Then I got a script for the bullet itself which is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Bulletenemy : MonoBehaviour {
    6.  
    7.     public float speedshooting = 20f;
    8.     public int damage = 40;
    9.     public Rigidbody2D rb;
    10.     public GameObject impactEffect2;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         rb.velocity = transform.right * speedshooting;
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D HitInfo)
    18.     {
    19.         Enemy enemy = HitInfo.GetComponent<Enemy>();
    20.         if (enemy != null)
    21.         {
    22.             enemy.TakeDamage(damage);
    23.         }
    24.  
    25.         Instantiate(impactEffect2, transform.position, transform.rotation);
    26.  
    27.         Debug.Log(HitInfo.name);
    28.         Destroy(gameObject);
    29.     }
    30. }

    How do I get them to shoot off to left with this?

    All the best
     
  2. Exthase

    Exthase

    Joined:
    Feb 25, 2019
    Posts:
    6
    Hello Lotias90, i think you need to change the transform.right to transform.left.
    Code (CSharp):
    1.     // Use this for initialization
    2.     void Start () {
    3.         rb.velocity = transform.right * speedshooting;
    4.     }
    5.  
     
  3. Lotias90

    Lotias90

    Joined:
    Nov 11, 2015
    Posts:
    30
    I gave that a go but it complained and said something about that I couldnt use "left" there, which is why its all so confusing to me haha
     
  4. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Absolute newb here, but how about transform.forward.? Isn't that a proper construct for the transform method.
     
  5. Exthase

    Exthase

    Joined:
    Feb 25, 2019
    Posts:
    6
    Oh of course, you need to transform.right and then have a negative multiply with speedshooting.
     
    Cenphon likes this.
  6. Cenphon

    Cenphon

    Joined:
    Jul 18, 2019
    Posts:
    6
    transform.left doesn't exist. Multiply the transform.left by -1

    It is a proper construct, but only useful in 3D. transform.forward is the local z axis, and 2D only uses X and Y axes.
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well you may be better off setting a public static bool on your enemy script, in the Update() isFacingRight = true when facing right and false when facing left. Then in Start() on your bullet script use if/else to determine which way the bullet should fire.

    Additionally, I'd use
    Code (CSharp):
    1. rb.velocity = Vector2.right * speedshooting;
    2. rb.velocity = Vector2.left * speedshooting;
    rather than transform.left.