简体   繁体   中英

How can I change the time that it takes to an animation to run in Unity scripting C#?

I have the player, which is a cube and I want it to "jump" to a chosen empty object position.

I managed to move the player from it's original place with vector3.MoveTowards() , but at the same time I want to play an animation that shows how the cube jumps to the empty object position.

The problem here is that the empty object position will change so the distance from the cube to the empty object will be different. I believe I need to change the time that it takes the animation to complete, so it would pause and after that let the cube move just in a straight line.

I want the animation to take a longer or shorter time to run, given that the empty object position will always change...

You can use StartCoroutine in this case.

IEnumerator animated() {
        // Code here
        yield return new WaitUntil (() => stopanimated == true);
        // Run the code here
}

Or you can

IEnumerator animated() {
        // Code here  
        yield return new WaitForSeconds (1); // How much second to wait before execute the next line code.
        // Run code here
}

And how to call it use :

StartCoroutine (Animated ());

For detail documentation coroutine here :

https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

This StartCoroutine is use to pause a code for some time before it execute.

And

To change the speed animated :

See this post : http://answers.unity3d.com/questions/950205/how-to-change-speed-of-animation-in-c.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM