为了账号安全,请及时绑定邮箱和手机立即绑定

随时间移动游戏对象

随时间移动游戏对象

SMILET 2019-07-02 14:36:18
随时间移动游戏对象我正在从Swift SpriteKit背景中学习UnityofSpriteKit,其中移动一个精灵的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)我想知道一种等效或类似的方法,可以在特定的时间内(例如,第二次)将sprite移动到一个特定的位置,并且在UPDATE函数中不需要调用这个延迟。
查看完整描述

3 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

gjttt 1的答案是接近的,但缺少重要的功能和使用WaitForSeconds()用于移动游戏对象是不能接受的。您应该使用LerpCoroutineTime.deltaTime..你必须了解这些东西,才能从统一脚本中制作动画。

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


查看完整回答
反对 回复 2019-07-02
?
慕莱坞森

TA贡献1810条经验 获得超4个赞

git 1的答案是好的,但是如果您不想使用礼貌,还有另一个解决方案。

你可以用InvokeRepeating反复触发一个函数。

float duration; //duration of movementfloat durationTime; //this will be the value used to check if Time.
time passed the current duration setvoid 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;
    }}


查看完整回答
反对 回复 2019-07-02
  • 3 回答
  • 0 关注
  • 396 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信