Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move GameObject over time

Tags:

c#

unity3d

unity5

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 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)

I would like to know an equivalent or similar way of moving a sprite to a specific position for a specific duration (say, a second) with a delay that doesn't have to be called in the update function.

like image 554
Nullititiousness Avatar asked Apr 25 '16 20:04

Nullititiousness


3 Answers

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

like image 126
Programmer Avatar answered Oct 09 '22 00:10

Programmer


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;
    }
}
like image 27
Mennolp Avatar answered Oct 09 '22 01:10

Mennolp


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

like image 22
Danny Herbert Avatar answered Oct 09 '22 01:10

Danny Herbert