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

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

[复制链接]
may    

8830

主题

80

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52328
精华
343

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

跳转到指定楼层
楼主
发表于 2016-1-16 03:30:25 |只看该作者 |倒序浏览
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();
        }
    }
}

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

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

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-6-11 11:03 , Processed in 0.086125 second(s), 28 queries .

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

© 2008-2019 Narkii Inc.

回顶部