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): using System.Collections; using System.Linq; using UnityEngine; namespace Assets.Scripts { [RequireComponent(typeof(Player))] public class PlayerShootController : ShootControllerBase { [SerializeField] private int _numberOfBombs; public int NumberofBombs { get { return _numberOfBombs; } set { _numberOfBombs = value; } } [SerializeField] private PlayerHeadController _headObject; public PlayerHeadController HeadObject { get { return _headObject; } set { _headObject = value; } } [SerializeField] private Bomb _bombPrefab; public Bomb BombPrefab { get { return _bombPrefab; } set { _bombPrefab = value; } } public bool IsShooting { get; private set; } private KeyCode _shootKey; private Vector2 _shootDirection; private Player _player; public override void Start() { base.Start(); _player = GetComponent<Player>(); } public override void Update() { base.Update(); if (IsShooting) { SetHeadDirection(_shootKey); return; } if (InputHelpers.IsAnyKey(KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow)) { if (Input.GetKey(KeyCode.UpArrow)) { _shootDirection = new Vector2(0, BulletSpeed); _shootKey = KeyCode.UpArrow; } else if (Input.GetKey(KeyCode.DownArrow)) { _shootDirection = new Vector2(0, -BulletSpeed); _shootKey = KeyCode.DownArrow; } else if (Input.GetKey(KeyCode.LeftArrow)) { _shootDirection = new Vector2(-BulletSpeed, 0); _shootKey = KeyCode.LeftArrow; } else if (Input.GetKey(KeyCode.RightArrow)) { _shootDirection = new Vector2(BulletSpeed, 0); _shootKey = KeyCode.RightArrow; } StartCoroutine(Shoot()); } if (NumberofBombs > 0 && InputHelpers.IsAnyKeyDown(KeyCode.LeftShift, KeyCode.RightShift, KeyCode.E)) { var bomb = (Bomb) Instantiate(BombPrefab); bomb.transform.position = transform.position; NumberofBombs--; } } IEnumerator Shoot() { IsShooting = true; while (Input.GetKey(_shootKey)) { var bullet = (Rigidbody2D)Instantiate(BulletPrefab); bullet.GetComponent<BulletScript>().Shooter = transform.gameObject; bullet.transform.position = transform.position; if (_shootDirection.y > 0) { bullet.transform.Rotate(0, 0, -90); } else if (_shootDirection.y < 0) { bullet.transform.Rotate(0, 0, 90); } else if (_shootDirection.x > 0) { TransformHelpers.FlipX(bullet.gameObject); } bullet.AddForce(_shootDirection); bullet.AddForce(_player.GetComponent<Rigidbody2D>().GetPointVelocity(_player.transform.position) * 0.02f); if (ShootClips.Any()) { var clipToPlay = ShootClips[Random.Range(0, ShootClips.Count)]; clipToPlay.pitch = Random.Range(MinShootPitch, MaxShootPitch); clipToPlay.Play(); } yield return new WaitForSeconds(ShootingSpeed); } IsShooting = false; //Reset head flipping if (_headObject.transform.localScale.x < 0) { TransformHelpers.FlipX(_headObject.gameObject); } } private void SetHeadDirection(KeyCode shootKey) { switch (shootKey) { case KeyCode.UpArrow: _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Up); break; case KeyCode.DownArrow: _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Down); break; case KeyCode.LeftArrow: _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Left); break; case KeyCode.RightArrow: _headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Right); break; } } } }
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!
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
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!