首页 > 解决方案 > 为什么我的子弹没有使用 rb.velocity 移动?

问题描述

我正在尝试自己制作一个小型太空射击游戏,但在尝试制作子弹预制件时遇到了问题。我在 start 方法中设置它的速度,当我按下 play 时它根本没有移动。

public float speed;
Rigidbody2D rb;

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    rb.velocity = transform.forward * speed;
}

我也在我的播放器脚本中使用 rb.velocity 来移动他,它工作得很好。

标签: c#unity3dscriptingbullet

解决方案


它不工作,因为你把 rb.velocity = transform.forward * speed; 进入启动功能。

试试这个脚本

public float speed;
Rigidbody2D rb;

void Start()
{
    rb = GetComponent<Rigidbody2D>();

}

void Update()
{
    rb.velocity = transform.forward * speed;
}

推荐阅读