Search Unity

Bullet doens't move

Discussion in 'Scripting' started by McTigo, Jan 18, 2020.

  1. McTigo

    McTigo

    Joined:
    Nov 4, 2019
    Posts:
    2
    I am working on a 2D, topdown, roguelike game but i got stuck when i tried to make a shooting system.
    when i run it and press the arrow keys, the bullet spawns and points in the right direction. It juste doesn't move in any way.
    This is my code and i hope someone can figure it out. Btw, everything is connected. The player and the bullet prefab.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Linq;
    3. using UnityEngine;
    4.  
    5. namespace Assets.Scripts
    6. {
    7.     [RequireComponent(typeof(Player))]
    8.     public class PlayerShootController : ShootControllerBase
    9.     {
    10.         [SerializeField]
    11.         private int _numberOfBombs;
    12.         public int NumberofBombs
    13.         {
    14.             get { return _numberOfBombs; }
    15.             set { _numberOfBombs = value; }
    16.         }
    17.        
    18.  
    19.         [SerializeField]
    20.         private PlayerHeadController _headObject;
    21.         public PlayerHeadController HeadObject
    22.         {
    23.             get { return _headObject; }
    24.             set { _headObject = value; }
    25.         }
    26.  
    27.         [SerializeField]
    28.         private Bomb _bombPrefab;
    29.         public Bomb BombPrefab
    30.         {
    31.             get { return _bombPrefab; }
    32.             set { _bombPrefab = value; }
    33.         }
    34.  
    35.         public bool IsShooting { get; private set; }
    36.         private KeyCode _shootKey;
    37.         private Vector2 _shootDirection;
    38.  
    39.         private Player _player;
    40.  
    41.         public override void Start()
    42.         {
    43.             base.Start();
    44.             _player = GetComponent<Player>();
    45.         }
    46.  
    47.         public override void Update()
    48.         {
    49.             base.Update();
    50.  
    51.             if (IsShooting)
    52.             {
    53.                 SetHeadDirection(_shootKey);
    54.                 return;
    55.             }
    56.  
    57.             if (InputHelpers.IsAnyKey(KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow))
    58.             {
    59.                 if (Input.GetKey(KeyCode.UpArrow))
    60.                 {
    61.                     _shootDirection = new Vector2(0, BulletSpeed);
    62.                     _shootKey = KeyCode.UpArrow;
    63.                 }
    64.                 else if (Input.GetKey(KeyCode.DownArrow))
    65.                 {
    66.                     _shootDirection = new Vector2(0, -BulletSpeed);
    67.                     _shootKey = KeyCode.DownArrow;
    68.                 }
    69.                 else if (Input.GetKey(KeyCode.LeftArrow))
    70.                 {
    71.                     _shootDirection = new Vector2(-BulletSpeed, 0);
    72.                     _shootKey = KeyCode.LeftArrow;
    73.                 }
    74.                 else if (Input.GetKey(KeyCode.RightArrow))
    75.                 {
    76.                     _shootDirection = new Vector2(BulletSpeed, 0);
    77.                     _shootKey = KeyCode.RightArrow;
    78.                 }
    79.                 StartCoroutine(Shoot());
    80.             }
    81.             if (NumberofBombs > 0 && InputHelpers.IsAnyKeyDown(KeyCode.LeftShift, KeyCode.RightShift, KeyCode.E))
    82.             {
    83.                 var bomb = (Bomb) Instantiate(BombPrefab);
    84.                 bomb.transform.position = transform.position;
    85.                 NumberofBombs--;
    86.             }
    87.         }
    88.  
    89.         IEnumerator Shoot()
    90.         {
    91.             IsShooting = true;
    92.             while (Input.GetKey(_shootKey))
    93.             {
    94.                 var bullet = (Rigidbody2D)Instantiate(BulletPrefab);
    95.                 bullet.GetComponent<BulletScript>().Shooter = transform.gameObject;
    96.                 bullet.transform.position = transform.position;
    97.                 if (_shootDirection.y > 0)
    98.                 {
    99.                     bullet.transform.Rotate(0, 0, -90);
    100.                 }
    101.                 else if (_shootDirection.y < 0)
    102.                 {
    103.                     bullet.transform.Rotate(0, 0, 90);
    104.                 }
    105.                 else if (_shootDirection.x > 0)
    106.                 {
    107.                     TransformHelpers.FlipX(bullet.gameObject);
    108.                 }
    109.                 bullet.AddForce(_shootDirection);
    110.                 bullet.AddForce(_player.GetComponent<Rigidbody2D>().GetPointVelocity(_player.transform.position) * 0.02f);
    111.  
    112.                 if (ShootClips.Any())
    113.                 {
    114.                     var clipToPlay = ShootClips[Random.Range(0, ShootClips.Count)];
    115.                     clipToPlay.pitch = Random.Range(MinShootPitch, MaxShootPitch);
    116.                     clipToPlay.Play();
    117.                 }
    118.  
    119.                 yield return new WaitForSeconds(ShootingSpeed);
    120.             }
    121.             IsShooting = false;
    122.  
    123.             //Reset head flipping
    124.             if (_headObject.transform.localScale.x < 0)
    125.             {
    126.                 TransformHelpers.FlipX(_headObject.gameObject);
    127.             }
    128.         }
    129.  
    130.         private void SetHeadDirection(KeyCode shootKey)
    131.         {
    132.             switch (shootKey)
    133.             {
    134.                 case KeyCode.UpArrow:
    135.                     _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Up);
    136.                     break;
    137.                 case KeyCode.DownArrow:
    138.                     _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Down);
    139.                     break;
    140.                 case KeyCode.LeftArrow:
    141.                     _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Left);
    142.                     break;
    143.                 case KeyCode.RightArrow:
    144.                     _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Right);
    145.                     break;
    146.             }
    147.         }
    148.     }
    149. }
    150.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    Are you setting
    _shootDirection
    nonzero? When you fire the thing, start using
    Debug.Log()
    to spit out the
    _shootDirection
    value, see if it is reasonable.

    I also don't see where you set
    BulletSpeed
    which is in turn used to set
    _shootDirection
    . Is that nonzero? Print it and see!
     
  3. McTigo

    McTigo

    Joined:
    Nov 4, 2019
    Posts:
    2

    hi, thanks for the awnser. i can see what you mean but cant get it right. is it possible that you cchange the wrong parts with how it works?

    thanks,
    Tigo Westerkamp
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    That's not really how engineering works, sorry. I can merely suggest approaches and techniques you can use to gain more insight into what is failing. Only you have the entire project, and you need to start isolating the most-likely reasons the shot is not moving. Here are some:

    1. the speed is zero (as I said above)
    2. the script is disabled
    3. the script is destroyed, the bullet is remaining "Stranded"
    4. the bullet has physics and it is stuck on something.
    5. etc. etc. etc

    I mean there's like 57000 ways it could go wrong, but the above are by far the most common. Your job as a software engineer is to isolate what the actual problem is and fix it, and that is where my suggestions above are intended to guide you.

    Good luck!