繁体   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