我正在制作一个 2d 俯视射击游戏,但我无法移动子弹


public Rigidbody2D rb;
public Movement mv;
public GameObject shotPrefab;
public Transform FirePoint;
public int direction;
// Update is called once per frame
void Update()
{

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

if(Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}

rb.velocity = transform.up * speed*Time.deltaTime;


}
void Shoot()
{
Instantiate(shotPrefab, FirePoint.position, FirePoint.rotation);


}

这是射击预制子弹的代码,即使预制子弹生成它确实移动了,我尝试了其他语法,如transform .translate,rb。

你必须将力应用于新实例化的对象,而不是预制件。

void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Shoot();
}
}
void Shoot() {
GameObject shot = Instantiate(shotPrefab, FirePoint.position, FirePoint.rotation);
Rigidbody2D rb = shot.GetComponent<Rigidbody2D>();
rb.AddForce(transform.up * speed, ForceMode2D.Impulse);
}

最新更新