纳金网
标题:
树木倒下后在重新升起的代码
[打印本页]
作者:
may
时间:
2016-1-16 03:30
标题:
树木倒下后在重新升起的代码
using
UnityEngine;
using
System.Collections;
/// <summary>
///树先旋转平行地面,下降,在升起
///树倒下,隐藏地面,然后慢慢长起来
/// </summary>
public
class
TreeDeath : MonoBehaviour{
Quaternion wantQ ;
//想转到的Quaternion
Quaternion BeforeQ;
Vector3 DownP;
Vector3 BeforeP;
float
speed = 0.7f;
//倒下的速度
bool
isDown =
false
;
//是否开始下降
bool
isUp =
false
;
public
void
Death()
{
BeforeQ = transform.rotation;
BeforeP = transform.position;
Vector3 dir =transform.rotation *
new
Vector3(Random.Range(-1f,1f),0f,Random.Range(-1f,1f))
* Random.Range(0,360) ;
//旋转使用的轴方向
wantQ= Quaternion.AngleAxis(120, dir) * transform.rotation;
StartCoroutine (
"RoTation"
);
}
IEnumerator RoTation()
{
for
(;;)
{
if
(isUp)
{
if
( transform.position.y>=BeforeP.y )
//判断树是否升起到原来的高度
{
gameObject.tag = Tags.Tree;
//这里使得树变成可攻击对象
isUp =
false
;
isDown =
false
;
GetComponent<LifeState>().Upgrade();
//这里刷新血量
yield
break
;
}
transform.position =
new
Vector3(transform.position.x,transform.position.y+Time.deltaTime*speed,transform.position.z);
}
else
{
if
(isDown==
false
)
{
//判断树是否已经倒了,树平行地面
if
((
int
)transform.localEulerAngles.x==77 || (
int
)transform.localEulerAngles.x==291
|| (
int
)transform.localEulerAngles.z==77 || (
int
)transform.localEulerAngles.z==291)
{
isDown =
true
;
continue
;
}
transform.rotation = Quaternion.Lerp(
this
.transform.rotation, wantQ, Time.deltaTime * speed);
}
else
{
if
( BeforeP.y -transform.position.y >5)
//判断树是否下降到了5米
{
gameObject.transform.rotation =BeforeQ;
gameObject.tag =Tags.TreeGrowth;
//这里使得树变成不可攻击对象
isUp =
true
;
continue
;
}
transform.position =
new
Vector3(transform.position.x,transform.position.y-Time.deltaTime*speed,transform.position.z);
}
}
yield
return
new
WaitForEndOfFrame();
}
}
}
欢迎光临 纳金网 (http://old.narkii.com/club/)
Powered by Discuz! X2.5