1 回答

TA貢獻1886條經驗 獲得超2個贊
您編寫的代碼一旦工作,將無限加速剛體。您需要將速度限制在最大速度:http ://answers.unity.com/answers/330805/view.html
rigidbody.AddForce(new Vector2(0, 1) * thrust * Time.deltaTime);
if (rigidbody.velocity.magnitude > topSpeed)
rigidbody.velocity = rigidbody.velocity.normalized * topSpeed;
如果您希望它立即將速度設置為固定值,那么您可以在每一幀上設置速度:
https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
void FixedUpdate()
{
if (Input.GetButtonDown("Jump"))
{
// the cube is going to move upwards in 10 units per second
rb2D.velocity = new Vector3(0, 10, 0);
moving = true;
Debug.Log("jump");
}
if (moving)
{
// when the cube has moved over 1 second report it's position
t = t + Time.deltaTime;
if (t > 1.0f)
{
Debug.Log(gameObject.transform.position.y + " : " + t);
t = 0.0f;
}
}
}
您的代碼沒有顯示它,所以如果您還沒有這樣做,您需要確保它rb2D實際上設置為您要操作的對象上的 Rigidbody2d。例如,通過在 start 方法中執行:
void Start()
{
rb2D = gameObject.GetComponent<Rigidbody2D>();
}
- 1 回答
- 0 關注
- 296 瀏覽
添加回答
舉報