Move GameObject over time

后端 未结 3 831
梦毁少年i
梦毁少年i 2020-11-22 09:02

I am learning Unity from a Swift SpriteKit background where moving a sprite\'s x Position is as straight forward as an running an action as below:

let moveLe         


        
3条回答
  •  难免孤独
    2020-11-22 09:24

    The answer of git1 is good but there is another solution if you do not want to use couritines.

    You can use InvokeRepeating to repeatedly trigger a function.

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

提交回复
热议问题