[英]How can I make the player continue floating from the spot where I paused it from?
我試圖讓我的播放器上下浮動,但當我按住點擊時會暫停。 釋放左鍵后,我想讓播放器繼續從同一個 position 浮動。 到目前為止,我的代碼可以完成所有這些,除了讓播放器繼續從它停止的 position 浮動。 相反,玩家會停下來,但如果我一開始沒有停止它,它會繼續從它的位置漂浮。
public float amplitude = 0.5f;
public float frequency = 1f;
void Start()
{
rigidbody = GetComponent<Rigidbody2D>();
// Store the starting position & rotation of the object
posOffset = transform.position;
isClick = false;
}
void Update()
{
if (Input.GetMouseButton(0))
{
isClick = true;
}
if (Input.GetMouseButtonUp(0))
{
isClick = false;
}
if (isClick)
{
rigidbody.constraints = RigidbodyConstraints2D.FreezePositionY;
curPos = GetComponent<Rigidbody2D>().position;
}
if (!isClick)
{
tempPos = posOffset;
tempPos.y = Mathf.Sin(Time.fixedTime * Mathf.PI * frequency) * amplitude;
transform.position = tempPos;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.