简体   繁体   English

我怎样才能让播放器从我暂停的地方继续漂浮?

[英]How can I make the player continue floating from the spot where I paused it from?

I am trying to make my player float up and down but pause when I hold click.我试图让我的播放器上下浮动,但当我按住点击时会暂停。 Upon releasing left click, I want to make the player continue floating from that same position.释放左键后,我想让播放器继续从同一个 position 浮动。 So far my code can do all of that except make the player continue floating from the position that it was stopped at.到目前为止,我的代码可以完成所有这些,除了让播放器继续从它停止的 position 浮动。 Instead, the player stops but then continues floating from where it would be if I hadn't stopped it in the first place.相反,玩家会停下来,但如果我一开始没有停止它,它会继续从它的位置漂浮。

        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;
            }
        }

You could achieve this by using the您可以通过使用

Time.timeScale = 0;

But be aware that this will stop the Time.deltaTime, but you could use insetead Time.fixedDeltaTime;但请注意,这将停止 Time.deltaTime,但您可以使用 insetead Time.fixedDeltaTime;

Check the docs检查文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM