如何通过物理改变子弹射击速度并保持子弹射击距离?

问题描述 投票:-1回答:1
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEditor;
using UnityEngine;

public class ShootBullets : MonoBehaviour
{
    public float bulletDistance;
    public bool automaticFire = false;
    public float fireRate;
    public Rigidbody bullet;

    private float gunheat;
    private bool shoot = false;
    private GameObject bulletsParent;
    private GameObject[] startpos;

    // Start is called before the first frame update
    void Start()
    {
        bulletsParent = GameObject.Find("Bullets");
        startpos = GameObject.FindGameObjectsWithTag("Pod_Weapon");
    }

    void Fire()
    {
        for (int i = 0; i < startpos.Length; i++)
        {
            Rigidbody bulletClone = (Rigidbody)Instantiate(bullet, startpos[i].transform.position, startpos[i].transform.rotation, bulletsParent.transform);

            bulletClone.velocity = transform.forward * bulletDistance;
            // The bullet or any ammo/weapon should be on Y rotation = 0
            Destroy(bulletClone.gameObject, 0.5f);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (automaticFire == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Fire();
            }
        }
        else
        {
            if (shoot == true)
            {
                    Fire();

                    shoot = false;
            }
        }

        if (gunheat > 0) gunheat -= Time.deltaTime;

        if (gunheat <= 0)
        {
            shoot = true;
            gunheat = fireRate;
        }
    }
}

我想保持这一行的距离:

bulletClone.velocity = transform.forward * bulletDistance;

还可以控制子弹速度。也许在使用物理学时有问题吗?我的问题是子弹射得太快。

问题是是否有可能使用物理学来控制速度并保持距离?是否应该从Update或FixedUpdate调用Fire函数?

c# unity3d
1个回答
0
投票

我的意思是“终生”,但没关系。该速度目前仅设置一次,因此仅取决于您发射子弹的距离。不是到目标的距离。

如果要在方程中保持距离但要减小结果,是否考虑过除速度? IE浏览器添加这样的东西

bulletClone.velocity = transform.forward * bulletDistance / 3;

您还可以将速度与最大值进行比较,并决定选择哪个IE

if(bulletDistance < 30)
{
bulletClone.velocity = transform.forward * bulletDistance;
}
else
{
bulletClone.velocity = transform.forward * 30
}

您可以将速度设置为任何所需的速度。

© www.soinside.com 2019 - 2024. All rights reserved.