簡體   English   中英

我怎樣才能讓播放器從我暫停的地方繼續漂浮?

[英]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;
            }
        }

您可以通過使用

Time.timeScale = 0;

但請注意,這將停止 Time.deltaTime,但您可以使用 insetead Time.fixedDeltaTime;

檢查文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM