1
\$\begingroup\$

I have one PlatformManager (Script Attached)

One is Position-1

One is Position-2

One is MovingPlatform

I want. MovingPlatform move Position-1 to Position-2 then wait for 5 Sec

then MovingPlatform move Position-2 to Position-1 then wait for 5 Sec

Here is code for PlatformManager & its working fine without Wait

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
    public Transform movingPlatform;
    public Transform position1;
    public Transform position2;
    public Vector3 newPosition;
    public string currentState;
    public float smooth;
    public float resetTime;

    private void Start()
    {
        ChangeTarget();
    }

    private void FixedUpdate()
    {
        movingPlatform.position = Vector3.Lerp(movingPlatform.position, newPosition, smooth * Time.deltaTime);
    }

    void ChangeTarget()
    {
        if (currentState == "Moving To Position 1")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
        }
        else if (currentState == "Moving To Position 2")
        {
            currentState = "Moving To Position 1";
            newPosition = position1.position;
        }
        else if (currentState == "")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
        }
        Invoke("ChangeTarget", resetTime);
    }
}

I have tried this code but MovingPlatform didnt Wait

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMovingPlatform : MonoBehaviour
{
    public Transform movingPlatform;
    public Transform position1;
    public Transform position2;
    public Vector3 newPosition;
    public string currentState;
    public float smooth;
    public float resetTime;

    IEnumerator Start()
    {
        while (true)
        {
            yield return StartCoroutine(ChangeTarget());
            yield return new WaitForSeconds(3.0f);
        }
    }

    private void FixedUpdate()
    {
        movingPlatform.position = Vector3.Lerp(movingPlatform.position, newPosition, smooth * Time.deltaTime);
    }

    IEnumerator ChangeTarget()
    {
        if (currentState == "Moving To Position 1")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
        }
        else if (currentState == "Moving To Position 2")
        {
            currentState = "Moving To Position 1";
            newPosition = position1.position;
        }
        else if (currentState == "")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
        }
        Invoke("ChangeTarget", resetTime);
        yield return null;
    }
}

Also i tried This but not success

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestMovingPlatform : MonoBehaviour
{
    public Transform movingPlatform;
    public Transform position1;
    public Transform position2;
    public Transform position3;
    public Vector3 newPosition;
    public string currentState;
    public float smooth;
    public float resetTime;

    IEnumerator Start()
    {
        while (true)
        {
            yield return StartCoroutine(ChangeTarget());
        }
    }

    private void FixedUpdate()
    {
        movingPlatform.position = Vector3.Lerp(movingPlatform.position, newPosition, smooth * Time.deltaTime);
    }

    IEnumerator ChangeTarget()
    {
        if (currentState == "Moving To Position 1")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
            yield return new WaitForSeconds(3);
        }
        else if (currentState == "Moving To Position 2")
        {
            currentState = "Moving To Position 1";
            newPosition = position1.position;
            yield return new WaitForSeconds(3);
        }
        else if (currentState == "")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
            yield return new WaitForSeconds(3);
        }
        Invoke("ChangeTarget", resetTime);
        yield return null;
    }
}

Here i attached Screenshot of Hirerchy & Inspector Here i attached Screenshot of Hirerchy & Inspector

Also i tried other different MovingPlatform Scripts but MovingPlatform's movement is jerky when player ride on it.

Only this script. MovingPlatform's movement is smooth when Player ride on it. So, I used this string script.

Anyone idea how to solve this?

\$\endgroup\$
1
  • \$\begingroup\$ You could simply use a while loop containing all the code inside your coroutine and have it wait for any amount to time you need by yield return new WaitForSeconds(time) \$\endgroup\$ Aug 22, 2017 at 6:03

1 Answer 1

-1
\$\begingroup\$

Call your co routine only once instead

 void Start()
{
    StartCoroutine(ChangeTarget());
 }

private void FixedUpdate()
{
    movingPlatform.position = Vector3.Lerp(movingPlatform.position, newPosition, smooth * Time.deltaTime);
}
IEnumerator ChangeTarget()
    {
        if (currentState == "Moving To Position 1")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
            yield return new WaitForSeconds(3);
        }
        else if (currentState == "Moving To Position 2")
        {
            currentState = "Moving To Position 1";
            newPosition = position1.position;
            yield return new WaitForSeconds(3);
        }
        else if (currentState == "")
        {
            currentState = "Moving To Position 2";
            newPosition = position2.position;
            yield return new WaitForSeconds(3);
        } 
        yield return null;
      StopCoroutine(ChangeTarget());
     StartCoroutine(ChangeTarget());

    }
\$\endgroup\$
2
  • \$\begingroup\$ The coroutine is being called twice in your code, so your commentary doesn't match. The important bit here is to not use Invoke as the invoke discards the resting IEnumerator that would have otherwise been used for the coroutine scheduling. \$\endgroup\$ Jul 13, 2017 at 18:24
  • \$\begingroup\$ i tried your answer. but still problem exists. \$\endgroup\$ Jul 14, 2017 at 11:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .