【问题标题】:Bullet doens't move. Unity子弹不动。统一
【发布时间】:2020-01-20 07:57:05
【问题描述】:

我正在开发一款 2D、自上而下的 roguelike 游戏,但当我尝试制作射击系统时却卡住了。 当我运行它并按箭头键时,子弹会产生并指向正确的方向。它只是不动。 这是我的代码,我希望有人能弄清楚。顺便说一句,一切都是相连的。播放器和子弹预制件。

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;
            }
        }
    }
}

【问题讨论】:

  • 当然,一旦你瞄准了你的子弹,你只需添加“前进”力量......
  • 嗨,感谢您的回复。但是我如何添加前向力以及代码中的确切位置?

标签: c# visual-studio unity3d


【解决方案1】:

试试这个:

bullet.AddForce(_shootDirection*moveSpeed*Time.deltaTime);

而不是这个:

bullet.AddForce(_shootDirection);
 bullet.AddForce(_player.GetComponent<Rigidbody2D>().GetPointVelocity(_player.transform.position) * 0.02f);

调整速度以获得所需的结果,如果你想让子弹跟随玩家,你需要每隔一定时间使用一个 couroutine 更新射击方向

【讨论】:

  • 嗨,感谢您的评论。但它不会改变任何东西,子弹不会移动一步。还有其他选择吗?
  • 哦,那是因为你只在调用 couroutine 时添加了一次力,如果你要使用 time.Deltatime 它需要在更新中我个人处理更新中的所有动作...... . 无论如何.. 你可以找到一种方法来调用它几次......你也可以给它添加足够的力......它会移动......另一个解决方案是为刚体设置速度:刚体。速度 = 新 Vector2(x, y) ;
猜你喜欢
  • 1970-01-01
  • 2016-09-19
  • 1970-01-01
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多