Move GameObject over time

后端 未结 3 825
梦毁少年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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:39

    You can use co-routines to do this. To do this, create a function that returns type IEnumerator and include a loop to do what you want:

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

    Then you can call it by using StartCoroutine(foo())

    This calls the function every frame but it picks up where it left off last time. So in this example it stops at yield return null on one frame and then starts again on the next: thus it repeats the code in the while loop every frame.

    If you want to pause for a certain amount of time then you can use yield return WaitForSeconds(3) to wait for 3 seconds. You can also yield return other co-routines! This means the current routine will pause and run a second coroutine and then pick up again once the second co-routine has finished.

    I recommend checking the docs as they do a far superior job of explaining this than I could here

    0 讨论(0)
  • 2020-11-22 09:40

    gjttt1's answer is close but is missing important functions and the use of WaitForSeconds() for moving GameObject is unacceptable. You should use combination of Lerp, Coroutine and Time.deltaTime. You must understand these stuff to be able to do animation from Script in 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;
    }
    

    Similar Question: SKAction.scaleXTo

    0 讨论(0)
提交回复
热议问题