在Unity中射击弹丸

马克休斯88

我正在尝试使用Unity在游戏中制造武器。我的子弹会生成,但我似乎无法施加力量实例化以使其实际射击。

我的武器脚本

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

public class Weapon : MonoBehaviour {

    public Rigidbody2D projectile;
    public float forceMultiplier;
    public Vector2 direction;

    public Transform firePoint;

    private float timeBtwShots;
    public float startTimeBtwShots;

    public void Fire(float force, Vector2 direction)
    {
        Instantiate(projectile, firePoint.position, transform.rotation);  
        projectile.AddForce(direction * force);
    }

    // Update is called once per frame
    void Update () {
        if (timeBtwShots <= 0)
        {
            if (Input.GetKeyDown(KeyCode.Return))
            {
                Fire(forceMultiplier, direction);
                timeBtwShots = startTimeBtwShots;
            }
        }
        else 
        {
            timeBtwShots -= Time.deltaTime;
        }
    }
}
塞莱亚斯

您需要将力添加到生成的对象而不是预制件。您的代码应如下所示:

    public void Fire(float force, Vector2 direction)
    {
        Rigidbody2D proj = Instantiate(projectile, firePoint.position, transform.rotation);  
        proj.AddForce(direction * force);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章