Search Unity

2D plateformer: how to shoot on the left ?

Discussion in '2D' started by Onchatte, Aug 21, 2017.

  1. Onchatte

    Onchatte

    Joined:
    Aug 14, 2017
    Posts:
    1
    Hello everybody,

    I'm a beginner in learning C# and I'm working on a 2D plateformer.
    I try to code the player's shots but it works only on the right. I tried to use the condition if (x < 0) / if (x > 0) in order to dissociate the left and right shots but the character can't shot at the 0 position (so, there are two script for the two kinds of shots left and right).

    Do you know how kind of condition I can use to indiquate the character position (on the left / on the right) ?

    Thanks !

    Here is the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ArmeGGAUCHE : MonoBehaviour
    6.  
    7. {
    8.  
    9.  
    10.     public GameObject projectilePrefab;
    11.  
    12.     private List<GameObject> Projectiles = new List<GameObject>();
    13.  
    14.     private float projectileVelocity;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.  
    20.         projectileVelocity = 9;
    21.  
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update ()
    26.     {
    27.         float x = Input.GetAxis ("Horizontal");
    28.  
    29.         if (x < 0)
    30.        
    31.         {
    32.             if (Input.GetButtonDown ("Fire1"))
    33.             {
    34.                 GameObject bullet = (GameObject)Instantiate (projectilePrefab, transform.position, Quaternion.identity);
    35.                 Projectiles.Add (bullet);
    36.             }
    37.                 for (int i = 0; i < Projectiles.Count; i++)
    38.             {
    39.                 GameObject goBullet = Projectiles [i];
    40.                 if (goBullet != null)
    41.                 {
    42.                     goBullet.transform.Translate(new Vector2(-7, 0) * Time.deltaTime * projectileVelocity);
    43.             }
    44.  
    45.  
    46.  
    47.         }
    48.     }
    49.  
    50. }
    51. }