我想知道如何暂停一个刚体2D 并恢复它。我尝试了许多不同的方法,但都没有奏效。我认为主要问题是当游戏暂停时,我的刚体 2D 会移动,所以我需要能够暂停刚体 2D,当未暂停时,刚体 2D 应该像以前一样继续移动。
这是我到目前为止的代码:
using UnityEngine;
using System.Collections;
public class Pause : MonoBehaviour {
private Rigidbody _rigidBody;
void Awake ()
{
_rigidBody = Ball.GetComponent<Rigidbody>();
}
private Vector3 _pausedVelocity;
private Vector3 _pausedAngularVelocity;
public void OnMouseEnter()
{
Debug.Log("Pause with velocity=" + _rigidBody.velocity + " & angularVelocity=" + _rigidBody.angularVelocity);
_pausedVelocity = _rigidBody.velocity;
_pausedAngularVelocity = _rigidBody.angularVelocity;
_rigidBody.isKinematic = true;
}
public void OnMouseExit()
{
_rigidBody.isKinematic = false;
_rigidBody.velocity = _pausedVelocity;
_rigidBody.angularVelocity = _pausedAngularVelocity;
Debug.Log("Resume with velocity=" + _rigidBody.velocity + " & angularVelocity=" + _rigidBody.angularVelocity);
}
}
基本上,这段代码附加到一个带有碰撞 2D 组件的空白处,当鼠标悬停在它上面时,它会打印暂停,当鼠标移开时,它会打印播放。所以一切正常,我只需要弄清楚如何暂停rigidbody2D!
提前致谢!