Unity 之 DoTween模拟人类的呼吸

//本篇需结合 DoTween 插件

//肺部模拟:
//      先吸后呼,先变大再缩小

float beginTime = 0; //默认时间从0开始
bool breathe_IN = true; //默认吸气是执行的
bool method_IN = true; //默认允许吸气方法

bool breathe_OUT = false; //在默认情况下,允许吸气了,那么呼气肯定不允许
bool method_OUT = false; //在默认情况下,吸气方法允许了,那么呼气方法肯定不允许

void Update(){

if (breathe_IN)
{
      beginTime += Time.deltaTime;
}

if( method_IN )
{
      BreatheIn();
      method_IN = false;
}

if (beginTime >= 2.1f)
// 当 吸入 到达2.1秒时,就不能再吸气了,接下来就是 呼气
{
      //这一块分析有问题,是达到了2.1秒之后开始呼吸,这不符合需求 ( 已修改 )
      breathe_IN = false;
      breathe_OUT = true;
      method_OUT = true;
}
if (breathe_OUT)
{
      beginTime -= Time.deltaTime;
}
if( method_OUT )
{
      BreatheOut();
      method_OUT = false;
}
if (beginTime <= 0f)
//当 呼气 达到2.1秒时,就不能再呼气了,接下来就是 吸气
{
      breathe_IN = true;
      breathe_OUT = false;
      method_IN = true;
}

} //这个结尾括号是update的

 void BreatheIn()
{
      transform.DOScale(2, 2.1f);
}

void BreatheOut()
{
      transform.DOScale(1, 2.1f);
}

一呼一吸,非常像人类的呼吸,心脏跳动也可以模拟成这样,当然是有规律的

你可能感兴趣的:(Unity,Unity,DoTween,Unity模拟呼吸)