随着时间的推移移动GameObject

时间:2016-04-25 20:17:44

标签: c# unity3d unity5

我正在从Swift SpriteKit背景学习Unity,其中移动精灵的x位置与运行动作一样直接如下:

let moveLeft = SKAction.moveToX(self.frame.width/5, duration: 1.0)
let delayAction = SKAction.waitForDuration(1.0)
let handSequence = SKAction.sequence([delayAction, moveLeft])
sprite.runAction(handSequence)

我想知道一个等效或类似的方法,将精灵移动到特定持续时间(例如,一秒)的特定位置,延迟不必在更新函数中调用。

3 个答案:

答案 0 :(得分:15)

gjttt1的答案很接近,但缺少重要的功能,使用WaitForSeconds()移动 GameObject 是不可接受的。您应该使用LerpCoroutineTime.deltaTime的组合。您必须了解这些内容才能从Unity中的脚本执行动画。

public GameObject objectectA;
public GameObject objectectB;

void Start()
{
    StartCoroutine(moveToX(objectectA.transform, objectectB.transform.position, 1.0f));
}


bool isMoving = false;

IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
{
    //Make sure there is only one instance of this function running
    if (isMoving)
    {
        yield break; ///exit if this is still running
    }
    isMoving = true;

    float counter = 0;

    //Get the current position of the object to be moved
    Vector3 startPos = fromPosition.position;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }

    isMoving = false;
}

类似问题:SKAction.scaleXTo

答案 1 :(得分:3)

git1的答案很好,但如果你不想使用couritines,还有另一个解决方案。

您可以使用InvokeRepeating重复触发功能。

float duration; //duration of movement
float durationTime; //this will be the value used to check if Time.time passed the current duration set

void Start()
{
    StartMovement();
}

void StartMovement()
{
    InvokeRepeating("MovementFunction", Time.deltaTime, Time.deltaTime); //Time.deltaTime is the time passed between two frames
    durationTime = Time.time + duration; //This is how long the invoke will repeat
}

void MovementFunction()
{
    if(durationTime > Time.time)
    {
        //Movement
    } 
    else 
    {
        CancelInvoke("MovementFunction"); //Stop the invoking of this function
        return;
    }
}

答案 2 :(得分:1)

您可以使用协同例程来执行此操作。为此,请创建一个返回类型IEnumerator的函数,并包含一个循环来执行您想要的操作:

private IEnumerator foo()
{
    while(yourCondition) //for example check if two seconds has passed
    {
        //move the player on a per frame basis.
        yeild return null;
    }
}

然后您可以使用StartCoroutine(foo())

来调用它

这会在每一帧调用该函数,但会在上次停止的位置调用该函数。因此,在此示例中,它在一帧上停留在yield return null,然后在下一帧停止:因此它每帧重复while循环中的代码。

如果您想暂停一段时间,可以使用yield return WaitForSeconds(3)等待3秒钟。您还可以yield return其他合作例程!这意味着当前例程将暂停并运行第二个协程,然后在第二个协同例程完成后再次启动。

我建议检查docs,因为他们在解释这方面做得比我在这里做得更好