Timescale the effect of deceleration on animation (RPM)

Source: Internet
Author: User

"The Legend of the turret" believes that game development is known,If you don't know, you're an old-age monster with a retreat , hahaha.
Here's a little joke.
in theThe legend of the turret,when a character releases a skill, all the other characters and animations of monsters will stop. When the character's skills are all set, the whole picture is back to normal.
in order to achieve the above functions I collected through the internet and my own groping out a set of solutions, now share to believe that it is useful to everyone.
First, to understand the next timescale (pause),
Pausing is a feature that often appears in the game,
in Unity3d, the handling of pauses is not ideal. The general practice is to set Time.timescale to 0.

timescale represents the scale of time lapse in the game. This parameter is used for slow motion effects.
for cases where timescale is set to 0, there is only one additional explanation.
in practical use, it is a fairly concise and non-side-effect method to implement slow motion effects by setting timescale.
However, when the timescale is set to the zero implementation pause, all time-related functions will be stopped because the time is no longer passing, and sometimes this is what we want, because after all it is a pause.
but the side effects come along, and the animations and particle effects will not play when paused (because it is time-dependent) and fixedupdate will no longer be called.
So how can I use timescale to achieve the above effect?
Just realtimesincestartup and timescale is irrelevant, that is to say realtimesincestartup is not affected by timescale.
so Realtimesincestartup is also a lifeline for solving animations and particle effects that are suspended. For unity Animations,
in each frame, the corresponding frame is searched according to the actual time and the method of sampling display is used to simulate the animation.
Let's look at some code

[C #]Plain Text view copy code
public static IEnumerator Play (this Animation Animation, string clipname, bool Ignoretimescale, Action OnComplete)
{
We don ' t want to use timescale and so we had to animate by frame.
if (Ignoretimescale)
{
Animationstate _currstate = Animation[clipname];
bool isplaying = true;

float _progresstime = 0F;
float _timeatlastframe = 0F;
float _timeatcurrentframe = 0F;
BOOL _inreverseplaying = false;

float _deltatime = 0F;
Animation. Play (Clipname);
_timeatlastframe = Time.realtimesincestartup;

while (isplaying) {
_timeatcurrentframe = Time.realtimesincestartup;
_deltatime = _timeatcurrentframe-_timeatlastframe;
_timeatlastframe = _timeatcurrentframe;

_progresstime + = _deltatime;

_currstate.normalizedtime = _inreverseplaying? 1.0f-(_progresstime/_currstate.length)
: _progresstime/_currstate.length;
Animation. Sample ();

if (_progresstime >= _currstate.length) {
Switch (_currstate.wrapmode) {
Case Wrapmode.loop:
Loop Anim, continue.
_progresstime = 0.0f;
Break
Case Wrapmode.pingpong:
Pingpong anim, reversing continue.
_progresstime = 0.0f;
_inreverseplaying =!_inreverseplaying;
Break
Case Wrapmode.clampforever:
Clampforever Anim, keep the last frame.
Break
Case Wrapmode.default:
We don ' t know how to handle it.
In the most time, this means it ' s a Once anim.
Animation should is played with wrap mode specified.
Debug.logwarning ("A Default Anim finished. Animation should is played with wrap mode specified. ");
IsPlaying = false;
Break
Default
Once Anim, kill it.
IsPlaying = false;
Break
}
}
Yield return new waitforendofframe ();
}
yield return null;

if (oncomplete! = null) {
OnComplete ();
}
} else {
if (oncomplete! = null) {
Debug.logwarning ("OnComplete" is not being called when you set \ "Ignoretimescale\" to true.) Use Unity ' s animation handler instead!);
Animation. Play (Clipname);
}
}
}


Note: The above code is only valid for animation, that's for Animator How do we play the animation normally in timescale =0?


[C #]Plain Text view copy code
public void Animatorplay (Animator _animator)
{
_animator.updatemode = Animatorupdatemode.unscaledtime;
The things you want to do
}

In fact, we can also set this directly in the panel


Simple setup in the above simple code and panel allows the animator to betimescale's influence.Isn't it very simple?

For the particle effect, the same timing,
The simulate method of particle system is used to simulate the particle state of the corresponding time to accomplish the effect.
For example, for legacy particles, the emitter continues to emit effectively when the timescale=0 is paused and shows the effect:

[C #]Plain Text view copy code
public class Unpauseparticlesystem:monobehaviour
{
private float _timeatlastframe;
Private Particlesystem _particlesystem;
private float _deltatime;

public void Awake () {
_timeatlastframe = Time.realtimesincestartup;
_particlesystem = gameobject.getcomponent<particlesystem> ();
}

public void Update () {
_deltatime = Time.realtimesincestartup-_timeatlastframe;
_timeatlastframe = Time.realtimesincestartup;
if (Time.timescale = = 0) {
_particlesystem.simulate (_deltatime,false,false);
_particlesystem.play ();
}
}

}




Run the following effect



At the beginningTimescale=1,2 Coube and characters as well as special effects can be played smoothly.

When I click the pause buttonTimescale= 0, all paused.


I'm adding a code to CUBE2 for the particle.


People will find that inTimescale=0, my special effects can play smoothly and properly,not affected byTimescalethe pause effect,the rest is still suspended.


I gave an empty object, bound to a code, to controlCube2 'sAnimationare equally unaffected, the results are as follows


(GIF map click to view)Now,InTimescale=0, my animation animation and special effects will play normally,not affected byTimescalethe pause effect,the rest is still suspended.


give me the character special effects also add aA code for the particle, and set my character aboveAnimatorHow to animate


The same animator and special effects of my charactercan play properly,not affected byTimescalethe pause effect,the rest is still suspended.

Timescale the effect of deceleration on animation (RPM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.