随时间移动游戏对象我正在从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个赞
WaitForSeconds()
Lerp
, Coroutine
Time.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;}
慕莱坞森
TA贡献1810条经验 获得超4个赞
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; }}
- 3 回答
- 0 关注
- 419 浏览
添加回答
举报
0/150
提交
取消