微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么我的弹丸游戏对象不移动?

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。