简体   繁体   中英

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. So far my code can do all of that except make the player continue floating from the position that it was stopped at. 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;

Check the docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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