为什么我的弹丸游戏对象不移动?

问题描述

我已经尝试了许多其他解决方案,但是没有一个对我有用。
即使我使用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);
    }
}

解决方法

只需按照这里的内容进行操作,并假设它是一个完整的示例,那么您的int变量ProjectileVelocity就没有分配。未分配的integer类型默认为0。乘以0得到一个0的值。如果您使用它作为力量,那么您的弹丸将无处可走。

,

更改代码并使用调试功能(感谢怪异)后,我能够向弹丸添加力,然后将刚体2d更改为动态,并将速度提高到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);
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...