纳金网

标题: 树倒下在重新升起的代码 [打印本页]

作者: 烟雨    时间: 2016-2-28 22:46
标题: 树倒下在重新升起的代码
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. ///树先旋转平行地面,下降,在升起
  5. ///树倒下,隐藏地面,然后慢慢长起来
  6. /// </summary>
  7. public class TreeDeath : MonoBehaviour{
  8.     Quaternion wantQ ;//想转到的Quaternion
  9.     Quaternion BeforeQ;
  10.     Vector3 DownP;
  11.     Vector3  BeforeP;
  12.     float speed = 0.7f;//倒下的速度
  13.     bool isDown =false;//是否开始下降
  14.     bool isUp =false;
  15.     public void Death()
  16.     {
  17.         BeforeQ = transform.rotation;
  18.         BeforeP = transform.position;
  19.         Vector3 dir =transform.rotation *new Vector3(Random.Range(-1f,1f),0f,Random.Range(-1f,1f))
  20.             * Random.Range(0,360) ;//旋转使用的轴方向
  21.         wantQ= Quaternion.AngleAxis(120, dir) * transform.rotation;
  22.         StartCoroutine ("RoTation");
  23.     }

  24.     IEnumerator  RoTation()
  25.     {
  26.         for(;;)
  27.         {

  28.             if(isUp)
  29.             {
  30.                 if( transform.position.y>=BeforeP.y )//判断树是否升起到原来的高度
  31.                 {
  32.                     gameObject.tag = Tags.Tree;//这里使得树变成可攻击对象
  33.                     isUp =false;
  34.                     isDown =false;
  35.                     GetComponent<LifeState>().Upgrade(); //这里刷新血量
  36.                     yield break;
  37.                 }
  38.                 transform.position = new Vector3(transform.position.x,transform.position.y+Time.deltaTime*speed,transform.position.z);
  39.                  
  40.             }
  41.             else
  42.             {
  43.                 if(isDown==false)
  44.                 {
  45.                     //判断树是否已经倒了,树平行地面
  46.                     if((int)transform.localEulerAngles.x==77 || (int)transform.localEulerAngles.x==291
  47.                     || (int)transform.localEulerAngles.z==77 || (int)transform.localEulerAngles.z==291)
  48.                     {
  49.                         isDown =true;
  50.                         continue;
  51.                     }
  52.                     transform.rotation = Quaternion.Lerp(this.transform.rotation, wantQ, Time.deltaTime * speed);
  53.                 }
  54.                 else
  55.                 {
  56.                     if( BeforeP.y -transform.position.y >5)//判断树是否下降到了5米
  57.                     {
  58.                         gameObject.transform.rotation =BeforeQ;
  59.                         gameObject.tag =Tags.TreeGrowth;//这里使得树变成不可攻击对象
  60.                         isUp =true;
  61.                         continue;
  62.                     }
  63.                     transform.position = new Vector3(transform.position.x,transform.position.y-Time.deltaTime*speed,transform.position.z);
  64.                 }
  65.             }

  66.             yield return new WaitForEndOfFrame();
  67.         }
  68.     }
  69. }
复制代码





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5