子弹没有以固定的速度加力?如何在2D中左右独立地正确拍摄?

问题描述

我对编码有点陌生,大约一年的经验。我不知道为什么我的脚本不会对以固定速度射击的子弹增加力量。这似乎取决于玩家移动的速度,但是我使用了2个不同的变量作为速度和MovementSpeed,所以我不知道为什么要这么做。我将如何解决这个问题?我也试图做到这一点,以便当角色面对该方向时我可以从左右两侧独立拍摄,但似乎不起作用。当我尝试修复它时,它会抛出错误,实例化时项目符号会保留在原位,或者项目符号总是在右侧。如果我做错了,正确的做法是什么?

using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehavIoUr
{
    public GameObject Bullet;
    public float sec = 2f;

    public float speed = 80;

    public GameObject Enemy;

    public Transform spawnPoint;

    public Rigidbody2D bulletRB;

    private float timeBtwShots;
    public float startTimeBtwShots;

    public AudioSource Gunshot;

    public Animator animator;

    private float vert;
    private float horiz;

    // Start is called before the first frame update
    void Start()
    {
        if (gameObject.tag == "Bullet")

            this.gameObject.SetActive(true);

        StartCoroutine(LateCall());

        IEnumerator LateCall()
        {
            yield return new WaitForSeconds(sec);

            this.gameObject.SetActive(false);
        }
       
        
    }

    void OnCollisionEnter2D(Collision2D Other)
    {
        Destroy(this.gameObject);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            this.gameObject.SetActive(false);
            other.gameObject.SetActive(false);
        } 
    }

    IEnumerator ShootingAnim()
    {
       
        timeBtwShots = startTimeBtwShots;
        Gunshot.Play();
        animator.SetBool("IsShooting",true);

        yield return new WaitForSeconds(0.2f);

        animator.SetBool("IsShooting",false);


    }

    void Update()
    {
        horiz = Input.GetAxis("Horizontal");
        vert = Input.GetAxis("Vertical");

        if (Input.GetMouseButtonDown(0))
        {
            Instantiate(bulletRB,spawnPoint.position,Quaternion.identity);
        }
    }

    void FixedUpdate()
    {
        

        if (timeBtwShots <= 0)
        {

                bulletRB.AddForce(transform.TransformDirection(new Vector2(horiz,vert)).normalized * speed);

                StartCoroutine(ShootingAnim());
            }
        else
        {
            timeBtwShots -= Time.deltaTime;
        }
        if (gameObject.tag == "Bullet")
        {
            Destroy(this.gameObject,1f);
        }
    }
}

解决方法

不要使用输入来确定子弹的方向,而要存储玩家所看的方向。添加一个名为“ currentDirection”的新Vector2,并将其默认为Vector2.right或其他名称。计算播放器上的移动时,请执行以下操作:

if (!Mathf.Approximately(horiz,0) || !Mathf.Approximately(vert,0))
    currentDirection = new Vector2(horiz,vert).normalized;

您会注意到第一行检查任一行是否都不为零。我这样做是因为如果两个输入均为0,则方向也为零。我们只想保存玩家输入的最后一个方向,而不是缺少方向。

接下来,子弹还应该具有方向变量。生成项目符号时,将项目符号的方向设置为currentDirection。您只想在生成子弹时执行此操作。如果每隔一帧将子弹的方向设置为玩家当前的输入,那么它将根据玩家的输入不断地改变方向。

然后只是告诉项目符号朝其指定的方向移动。人们通常会改变旋转方向以匹配方向并告诉其前进,但这取决于您希望艺术品寻找子弹的方式。


也非常重要,但又无关紧要:您需要将所有刚体代码放入FixedUpdate中。物理仅在固定更新期间发生,因此执行任何与物理无关的操作都可能导致问题。但是,这样做时,将输入检查保留在Update中。由于FixedUpdates并非在每一帧都发生,因此如果将它们放入FixedUpdate,可能会丢失输入。因此,添加一些输入变量,并在更新中添加

jumpInput = Input.GetButtonDown("Jump");
horiz = Input.GetAxis("Horizontal");
vert = Input.GetAxis("Vertical");

然后在FixedUpdate中,您的物理过程就像:

if (jumpInput && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
,

最后弄清楚了。我做了一个完整的重写,因为我使代码过于复杂。 这是最终的脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float MovementSpeed = 1;
    public float JumpForce = 1;
    public float jumpTime = 1f;

    public Rigidbody2D _rigidbody;

    private bool facingRight;

    public float sec = 2f;

    public Vector2 bulletPos;

    public GameObject bulletToRight,bulletToLeft;

    public Transform spawnPoint;

    public Rigidbody2D bulletRB;

    public AudioSource Gunshot;

    float horizontalMove = 0f;

    public float fireRate = 0.5f;
    float nextFire = 0f;

    // public AudioSource Jump;

    public Animator animator;

    void HandleMovement(float horizontal)
    {
        transform.position += new Vector3(horizontal,0) * Time.deltaTime * MovementSpeed;
        animator.SetFloat("velX",(horizontal));
    }

    void Flip(float horizontal)
    {
        if (horizontal < 0 && !facingRight || horizontal > 0 && facingRight)
        {
            facingRight = !facingRight;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;

            transform.position += new Vector3(horizontal,0) * Time.deltaTime * MovementSpeed;

            horizontalMove = Input.GetAxisRaw("Horizontal") * MovementSpeed;

            animator.SetFloat("velX",(horizontalMove));
        } 
    }

    IEnumerator ShootingAnim()
    {

        
        animator.SetBool("IsShooting",true);

        yield return new WaitForSeconds(0.2f);

        animator.SetBool("IsShooting",false);


    }

    void Update()
    {

        animator.SetFloat("velX",Mathf.Abs(horizontalMove));

        if (Input.GetButtonDown("Jump") && Mathf.Abs(_rigidbody.velocity.y) < 0.001f)
        {
            _rigidbody.AddForce(new Vector2(0,JumpForce),ForceMode2D.Impulse);
            animator.SetBool("IsJumping",true);
            // Jump.Play();
        }

        if (Mathf.Abs(_rigidbody.velocity.y) == 0f)
        {
            animator.SetBool("IsJumping",false);
        }

        if ((_rigidbody.velocity.x) >= 0.01f || (_rigidbody.velocity.x <= -0.01f)) 
        {
            animator.SetBool("IsRunning",true);
        }

        if (Input.GetMouseButtonDown(0) && Time.time > nextFire)
        {
            StartCoroutine(ShootingAnim());
            nextFire = Time.time + fireRate;
            Fire();

        }


        float horizontal = Input.GetAxis("Horizontal");

        HandleMovement(horizontal);

        Flip(horizontal);



    }

    void Fire()
    {
        bulletPos = spawnPoint.position;
        if (!facingRight)
        {
            bulletPos += new Vector2(+0.02f,0f);
            Instantiate(bulletToRight,bulletPos,Quaternion.identity);
            Gunshot.Play();
        } else
        {
            bulletPos += new Vector2(-0.02f,0f);
            Instantiate(bulletToLeft,Quaternion.identity);
            Gunshot.Play();
        }
        
    }
}

这是BulletScript:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
    public GameObject Bullet;

    public Transform spawnPoint;

    public Rigidbody2D bulletRB;

    public float velX = 5f;

    float velY = 0f;

    // Start is called before the first frame update
    void Start()
    {
        bulletRB = GetComponent<Rigidbody2D>();
    }

    void OnCollisionEnter2D(Collision2D Other)
    {
        Destroy(this.gameObject);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Enemy")
        {
            this.gameObject.SetActive(false);
            other.gameObject.SetActive(false);
        } 
    }

    private void Update()
    {

        bulletRB.velocity = new Vector2(velX,velY);
        Destroy(gameObject,3f);

    }
}