1
\$\begingroup\$

Hello hello :) Ive been attemping to move a platform (a door basically), and it has partually worked. What works so far is i can move an object from A to B and wait, however it will not move it towards C afterwards, here is my code (note that all of the Debug.Logs work). Here is my code.

 public class OpenDoor : MonoBehaviour
{
    public Transform origPos;
    public Transform targetPos;
    public float speed;
    public GameObject button;
    private ButtonScript bs;

    private void Start()
    {
        bs = button.GetComponent<ButtonScript>();

    }

    void Update()
    {
        if (bs.buttonActive) {
            Debug.Log("Button Active 2");
            StartCoroutine(MovePlatform());
            bs.buttonActive = false;
        }
    }


    IEnumerator MovePlatform()
    {
        float step = speed * Time.deltaTime;
        bool arrived = false;
        while (!arrived)
        {
            transform.position = Vector3.MoveTowards(transform.position, targetPos.position, step);
            if (Vector3.Distance(transform.position, targetPos.position) == 0)
            {
                Debug.Log("DISTANCE 0");
                arrived = true;
            }
            yield return null;
        }
        if (arrived) {
            Debug.Log("Arrived");
            yield return new WaitForSeconds(1);
            Debug.Log("Waited");
            transform.position = Vector3.MoveTowards(transform.position, origPos.position, step);
        }

    }
}

Any assisstance would be greatly appreciated :)

\$\endgroup\$
  • \$\begingroup\$ Looks like you forgot a second while loop to keep moving until you arrive at the second point. You don't need the if (arrived)— there's no way for the execution to get there until arrived is true anyway. \$\endgroup\$ – DMGregory May 25 at 18:45
  • \$\begingroup\$ @DMGregory the if arrived worked as the debugs were called, however i tested this out and added this line after the if arrived while (Vector3.Distance(transform.position, origPos.position) != 0) However it still does not work \$\endgroup\$ – ConnnK May 25 at 18:57
  • \$\begingroup\$ I didn't say it wouldn't work, I said it does nothing useful for you. It's a meaningless line of code to have there. Your while loop should go after the wait, not after the if. \$\endgroup\$ – DMGregory May 25 at 19:02
  • \$\begingroup\$ Okay deleted there. Still makes no difference, code optimisations dont really matter when it doesnt work in the first place \$\endgroup\$ – ConnnK May 25 at 19:33
  • 1
    \$\begingroup\$ I told you that not as an optimization, but as a sign that you don't understand what your program is doing, which suggests you need to take a step back and walk through your code step-by-step to make sure it's saying what you meant it to. \$\endgroup\$ – DMGregory May 25 at 19:41
3
\$\begingroup\$

You can do this more simply by having one coroutine that expresses the sequence of actions you want to perform:

IEnumerator MovePauseAndReturn() {   
    yield return MoveTo(targetPos);
    yield return new WaitForSeconds(1);
    yield return MoveTo(origPos);
}

And a workhorse that performs the move:

IEnumerator MoveTo(Transform destination) {
    while (transform.position != destination.position) {
        transform.position = Vector3.MoveTowards(transform.position, destination.position, speed * Time.deltaTime); 
        yield return null;
     }
}

Since the same code is doing the work of moving there and moving back, you don't have to repeat yourself, and you can't introduce bugs where one move works but the other doesn't.

Note that you shouldn't pre-cache the step at th start of the coroutine: because frame rate can vary, this can lead to you using too long a timestep on short frames or vice versa, if the frame you started on isn't the same length as every subsequent frame the coroutine runs.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks so much @DMGregory ! That works now, your the best man :) \$\endgroup\$ – ConnnK May 25 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.