Search Unity

[SOLVED] Enemy Only Shoots To The Right

Discussion in 'Scripting' started by ChristodoulosL, Mar 21, 2019.

  1. ChristodoulosL

    ChristodoulosL

    Joined:
    Mar 13, 2019
    Posts:
    6
    [Edit]: Solved! Turns out I had the player's weapon script still attached to the enemy

    Hi everyone! I've got a problem with the enemy weapon script for my game, as the enemy's cannonballs will only fire to the right hand side (I want them to fire to the left instead).

    Here is the relevant code:

    Code (CSharp):
    1.  
    2. public class ProjectileEnemy2 : MonoBehaviour
    3. {
    4.     public float lifeTime = 3.0f;
    5.  
    6.     public float moveSpeed = 40.0f;
    7.  
    8.     public float damage = 100.0f;
    9.  
    10.     Player target;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.         Destroy(this.gameObject, lifeTime);
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         Movement();
    21.     }
    22.  
    23.     private void Movement()
    24.     {
    25.             transform.position += Time.deltaTime * moveSpeed * transform.right;
    26.     }
    27.  
    28.     void OnTriggerEnter(Collider other)
    29.     {
    30.         if (other.transform.tag == "Player")
    31.         {
    32.             other.GetComponent<Player>().takeDamage(damage);
    33.             Destroy(this.gameObject);
    34.         }
    35.     }
    36. }
    37.  
    I think the problem lays in line 25. I've tried changing the '+' sign to a '-', I've tried * -1 *, and I've even tried transform.forward & transform.up but with no such luck.

    Thanks for any help
     
    Last edited: Mar 21, 2019
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,627
    (-1 * transform.right) should give you left but it sounds like you already tried that and it didn't work for some reason?

    Note that transform.right will give you the projectile's right, which may not necessarily be the enemy's right, or the right side of the screen, or whichever right you want the projectile to move in.
     
  3. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Show the code where you're instantiating your projectiles. Generally, you should have some transform and instantite projectile at that transform.position and rotation. After that, your projectile local "right" direction should be correct. You should not add factors like -1 or change projectile code by any means to make it fly another direction. The launcher of the projectile should define it's direction by common means.
     
  4. ChristodoulosL

    ChristodoulosL

    Joined:
    Mar 13, 2019
    Posts:
    6
    Thanks for your reply! You made me go into the Unity settings and mess around with the cannonball's rotation options in the inspector, and that's when I finally realised that I still had the player's weapon script attached! So thank you, I probably would've never checked that area!
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    usually you want to let projectiles move forwards (transform.forward). You should set the rotation to the correct direction when you create the projectile and then move it always forwards.
     
  6. ChristodoulosL

    ChristodoulosL

    Joined:
    Mar 13, 2019
    Posts:
    6
    This issue has now be
    I just solved the problem. Thank you for replying nonetheless, definitely make it a habit of using transform.forward from now on
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    In 2d game it's better to stick to transform.right, as in unity 2d things happens in xy plane. For most 2d games forward is actually transform.right, Contra for example, or Golden Axe. Use transform.forward in 3d and tranform.right in 2d.