查看: 585|回复: 0
打印 上一主题 下一主题

[其他] 树倒下在重新升起的代码

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-2-28 22:46:39 |只看该作者 |倒序浏览
  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. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-17 10:12 , Processed in 0.062856 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部