【问题标题】:Unity3d C# turret gun projectile won't moveUnity3d C#炮塔炮弹不会移动
【发布时间】:2014-11-06 20:03:40
【问题描述】:

我正在研究炮塔。除了将炮弹从炮塔枪管中射出外,我已经完成了所有工作。目前,唯一使用的枪管是

void Start(){
    firingPointRight = GameObject.FindGameObjectWithTag ("FiringPointRight").transform;
    firingPointLeft = GameObject.FindGameObjectWithTag ("FiringPointLeft").transform;
}

void Update() {
    if(Input.GetMouseButton(0)){
        count++;
        if (count >= maxCount)
        {
            count = 0;
            //Debug.Log ("FIRE");
            firingPointRight = GameObject.FindGameObjectWithTag ("FiringPointRight").transform;
            firingPointLeft = GameObject.FindGameObjectWithTag ("FiringPointLeft").transform;

            //  Create cannon muzzle fire effect.
            GameObject e1 = Instantiate (cannonMuzzleFire) as GameObject;
            e1.transform.position = firingPointRight.transform.position;
            e1.transform.rotation = firingPointRight.transform.rotation;

            Destroy (e1, 0.03f);

            GameObject e2 = Instantiate (cannonMuzzleFire) as GameObject;
            e2.transform.position = firingPointLeft.transform.position;
            e2.transform.rotation = firingPointLeft.transform.rotation;

            Destroy (e2, 0.03f);
            //-------------------------------------------

            //Left cannon. Fire projectile from cannon.

            //Debug.Log ("Firing Left");
            Rigidbody InstantiateedProjectile = Instantiate(cannonAmmo, firingPointLeft.transform.position, firingPointLeft.transform.rotation) as Rigidbody;
            if (InstantiateedProjectile != null)
            {
                print ("Firing projectile");
                InstantiateedProjectile.transform.Translate(Vector3.forward * cannonAmmoSpeed * Time.deltaTime);
            }

        }
    }

【问题讨论】:

    标签: c# unity3d game-physics


    【解决方案1】:

    弹丸有自己的脚本吗?您似乎没有为实例化的射弹设置任何速度。

    Translate() 除了立即设置新位置外,实际上并没有在移动方式上执行太多操作。如果弹丸附加了刚体组件,那么您可以直接设置刚体的速度。

    像这样:

    InstantiateedProjectile.velocity = Vector3.forward * cannonAmmoSpeed;
    

    此处不会使用 Time.deltaTime,因为它只需要包含在每帧执行的计算中。


    作为旁注,您可能希望考虑重用您的耀斑效果,而不是每次拍摄都产生一个新的。您可以使效果不可见并在拍摄之间重置它,这样可以提供更好的性能。

    【讨论】:

    • 感谢您的提示。我通过取消选中检查器中刚体的 Is Kinematic 复选框来使其工作。我也使用了弹丸速度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多