1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

MovingPlatform - move position1 to position2 then wait for 3 sec

Discussion in 'Programming/Internet' started by Alpesh Savaliya, Oct 8, 2018.

  1. 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 [​IMG]

    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?

    Login To add answer/comment
     

Share This Page