MovingPlatform - 将position1移动到position2然后等待3秒

时间:2017-07-13 09:52:55

标签: c# unity3d

我有一个PlatformManager(附加脚本)

一个是Position-1

一个是位置-2

一个是MovingPlatform

我想要。 MovingPlatform将Position-1移动到Position-2然后等待5秒

然后MovingPlatform将Position-2移动到Position-1然后等待5秒

这是PlatformManager&的代码。它的工作正常,没有等待

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

我试过这段代码,但MovingPlatform并没有等待

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

我也试过这个但没有成功

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

此外,我尝试了其他不同的MovingPlatform脚本,但当玩家乘坐它时,MovingPlatform的动作是生涩的。

只有这个脚本。当玩家骑行时,MovingPlatform的移动很顺畅。

任何人都想知道如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

首先为您的平台/ paddle状态创建一些枚举

enum PaddleState
{
    Retargetting,
    Moving,
    Stationary
}

现在为您的行为添加一些字段:

PaddleState paddleState = PaddleState.Stationary;
Vector3[] targets;
int currentTargetIdx = 0;
float stationaryFor = 0.0f;

当paddle处于正确状态时,将Update()方法更改为仅执行某些逻辑:

void Update()
{
    if(CheckState())
    {
        ChangeState();
    }
}

void CheckState()
{
    switch(paddleState)
    {
        case PaddleState.Retargetting: // choose new target and reset stationary timer
            {
                stationaryFor = 0.0f;
                if(targets.Length >= currentTargetIdx)
                {
                    currentTargetIdx = 0;
                }
                paddletarget = targets[currentTargetIdx++]; // post increment 
                return true;
            }

        case PaddleState.Moving: // move paddle and check the distance to target, if distance is less than x then go to the stationary state
            {
                paddle.position = Vector3.Lerp(paddle.position, targets[currentTargetIdx], smooth * Time.deltaTime);
                return Vector3.Distance(paddle.position, targets[currentTargetIdx]) < 0.5f; // arbitrary distance 
            }

         case PaddleState.Stationary: // do not move for 3 seconds
            {
                stationaryFor += Time.deltaTime;
                return stationaryFor >= 3.0f;
            }
    }
}

void ChangeState()
{
    // go to the next state
    paddleState = (int)paddleState + 1 > PaddleState.Stationary ? PaddleState.Retargeting : (PaddleState)((int)paddleState + 1);
}

答案 1 :(得分:0)

这是一个没有状态的小脚本,只是一个小程序来实现你想要的:

    // Drag & Drop the platform gameobject
    public Transform movingPlatform;

    // Drag & Drop all the GameObjects (empty) the platform must go to
    public Transform[] positions;

    private void Awake()
    {
        StartCoroutine( Run() );
    }

    private IEnumerator Run( float duration = 5f, float waitTime = 3f )
    {
        int index = 0;
        float time = 0;
        WaitForSeconds wait = new WaitForSeconds( waitTime );
        while( true )
        {
            Vector3 startPosition = positions[index].position;
            Vector3 endPosition = positions[ (index + 1) % positions.Length].position;
            for ( time = 0 ; time < duration ; time += Time.deltaTime )
            {
                movingPlatform.position = Vector3.Lerp( startPosition, endPosition, Mathf.SmoothStep( 0, 1, time ) );
                yield return null;
            }
            movingPlatform.position = endPosition;
            index = ( index + 1 ) % positions.Length;
            yield return wait;
        }
    }