0

我已经尝试了许多其他解决方案,但没有一个对我有用。
即使我使用AddForce.

这是代码

public Transform gunExitPoint;
public int ProjectileVelocity;
public GameObject projectilePref;

void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        GameObject shot = Instantiate(projectilePref, gunExitPoint.position, gunExitPoint.rotation);

        Rigidbody2D rb = shot.GetComponent<Rigidbody2D>();
        rb.AddForce(transform.forward * ProjectileVelocity * Time.deltaTime, ForceMode2D.Impulse);
    }
}
4

2 回答 2

0

只是按照你在这里的内容,假设它是一个完整的例子,你的int变量ProjectileVelocity是未分配的。未分配的integer类型默认为0. 乘以得到0一个0值。如果你用它作为你的力量,那么你的弹丸将不会去任何地方。

于 2020-08-25T14:05:33.933 回答
0

在更改我的代码并使用调试(感谢 gmiley)后,我能够将力添加到我的弹丸,然后我将rigidbody2d 更改为动态并将速度提高到 1000f。这是代码(不幸的是有点草率)

public Vector2 thrust;
public Transform gunExitPoint;
public int ProjectileVelocity = 1000;
public GameObject projectilePref;

// Start is called before the first frame update
void Start()
{
    
}


void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {

        GameObject shot = Instantiate(projectilePref, gunExitPoint.position, gunExitPoint.rotation);

        Rigidbody2D rb = shot.GetComponent<Rigidbody2D>();

        thrust = transform.up * ProjectileVelocity ;
        rb.AddForce(thrust, ForceMode2D.Impulse);
    }
}
于 2020-08-25T21:25:00.467 回答