动画系统

摘要:
玩家:使用System.Collections;使用System.Collections.Generic;使用UnityEngine;//此类需要声明属性System.Serializable,这意味着可序列化[System.Serializable]publicclassAnim{publicAnimationCliple;publicAnimationCliprunForw

游戏玩家:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//此类需要声明属性System.Serializble,表示可序列化
[System.Serializable]
public class Anim
{
    public AnimationClip idle;
    public AnimationClip runForward;
    public AnimationClip runBackward;
    public AnimationClip runRight;
    public AnimationClip runLeft;

}

public class PlayerCtrl : MonoBehaviour
{
    private float h = 0.0F;
    private float v = 0.0F;

    //必须先分配变量,之后才能使用常用组件
    private Transform tr;

    //移动速度变量
    public float moveSpeed = 10.0F;
    //旋转速度变量
    public float rotSpeed = 100.0F;

    //要显示到检视视图的动画类变量
    public Anim anim;
    //要访问下列3D模型Animation组件对象的变量
    public Animation _animation;

    // Start is called before the first frame update
    void Start()
    {
        //向脚本初始部分分配Transform组件
        tr = GetComponent<Transform>();

        _animation = GetComponentInChildren<Animation>();

        //_animation.clip = anim.idle;
        //_animation.Play();
    }

    // Update is called once per frame
    void Update()
    {
        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");

        //计算前后左右移动方向向量
        Vector3 moveDir = (Vector3.forward * v) + (Vector3.right * h);

        //Translate(移动方向*Time.deltaTime*位移值*速度,基础坐标)
        tr.Translate(moveDir.normalized * Time.deltaTime * moveSpeed, Space.Self);

        //以Vecter3.up轴为基准,以rotSpeed速度旋转
        tr.Rotate(Vector3.up * Time.deltaTime * rotSpeed * Input.GetAxis("Mouse X"));

        if (v >= 0.1F)
        {
            //前进动画
            _animation.CrossFade(anim.runForward.name, 0.3F);
        }
        else if (v <= -0.1F)
        {
            //后退动画
            _animation.CrossFade(anim.runBackward.name, 0.3F);
        }
        else if (h >= 0.1F)
        {
            //向右动画
            _animation.CrossFade(anim.runRight.name, 0.3F);
        }
        else if (h <= -0.1F)
        {
            //向左动画
            _animation.CrossFade(anim.runLeft.name, 0.3F);
        }
        else
        {
            //idle动画
            _animation.CrossFade(anim.idle.name, 0.3F);
        }
    }
}

敌人物体(NPC):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class MonsterCtrl : MonoBehaviour
{
    //声明表示怪兽状态信息的Enumerable变量
    public enum MonsterState { idle,trace,attack,die};
    //保存怪兽当前状态的Enum变量
    public MonsterState monsterState = MonsterState.idle;

    //为提高速度而向变量分配各种组件
    private Transform monsterTran;
    private Transform playerTran;
    private NavMeshAgent nvAgent;
    private Animator animator;

    //追击范围
    public float traceDist = 10.0F;
    //攻击范围
    public float attackDist = 2.0F;

    //怪兽是否死亡
    private bool isDie = false;

    // Start is called before the first frame update
    void Start()
    {
        //获取怪兽的Transform组件
        monsterTran = this.gameObject.GetComponent<Transform>();
        //获取玩家Transform组件
        playerTran = GameObject.FindWithTag("Player").GetComponent<Transform>();
        //获取NavMeshAgent组件
        nvAgent = this.gameObject.GetComponent<NavMeshAgent>();
        //获取Animator组件
        animator = this.gameObject.GetComponent<Animator>();

        //设置要追击对象的位置后,怪兽马上开始追击
        nvAgent.destination = playerTran.position;

        //运行定期检查怪兽当前状态的协程函数
        StartCoroutine(this.CheckMonsterState());

        //运行根据怪兽当前状态执行相应例程的协程函数
        StartCoroutine(this.MonsterAction());
    }

    //定期检查怪兽当前状态并更新monsterState值
    IEnumerator CheckMonsterState()
    {
        while (!isDie)
        {
            //等待0.2秒后执行后面的代码
            yield return new WaitForSeconds(0.2F);

            //测量怪兽与玩家之间的距离
            float dist = Vector3.Distance(playerTran.position, monsterTran.position);

            if (dist <= attackDist)
            {
                monsterState = MonsterState.attack;        //查看玩家是否进入攻击范围
            }
            else if (dist <= traceDist)
            {
                monsterState = MonsterState.trace;         //查看玩家是否进入追击范围
            }
            else
            {
                monsterState = MonsterState.idle;          //怪兽的状态为开始状态
            }
        }
    }

    //根据怪兽当前状态执行适当的动作
    IEnumerator MonsterAction()
    {
        while (!isDie)
        {
            switch (monsterState)
            {
                //idle状态
                case MonsterState.idle:
                    //停止追击
                    nvAgent.Stop();
                    //将Animator的IsTrace变量设置为false
                    animator.SetBool("IsTrace", false);
                    break;

                //追击状态
                case MonsterState.trace:
                    //传递要追击对象的位置
                    nvAgent.destination = playerTran.position;
                    //重新开始追击
                    nvAgent.Resume();
                    //将Animator的IsTrace变量设置为true
                    animator.SetBool("IsTrace", true);
                    break;
                
                //攻击状态
                case MonsterState.attack:
                    break;
                case MonsterState.die:
                    break;
                default:
                    break;
            }
            yield return null;
        }
    }
}

免责声明:文章转载自《动画系统》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[原]SQLite的学习系列之获取数据库版本Python实现 灰色关联分析 与结果可视化下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用Unity3D的50个技巧:Unity3D最佳实践!

关于这些技巧 这些技巧不可能适用于每个项目。 这些是基于我的一些项目经验,项目团队的规模从3人到20人不等; 框架结构的可重用性、清晰程度是有代价的——团队的规模和项目的规模决定你要在这个上面付出多少; 很多技巧是品味的问题(这里所列的所有技巧,可能有同样好的技术替代方案); 一些技巧可能是对传统的Unity开发的一个冲击。例如,使用prefab替代对象...

线程属性--十分重要的概念

http://blog.chinaunix.net/uid-23193900-id-3346199.html 一.线程属性 线程具有属性,用pthread_attr_t表示,在对该结构进行处理之前必须进行初始化,在使用后需要对其去除初始化。我们用pthread_attr_init函数对其初始化,用pthread_attr_destroy对其去除初始化。...

shell export 作用转载

shell 与 export命令用户登录到Linux系统后,系统将启动一个用户shell。在这个shell中,可以使用shell命令 或声明变量,也可以创建并运行shell脚本程序。运行shell脚本程序时,系统将创建一个子shell。 此时,系统中将有两个shell,一个是登录时系统启动的shell,另一个是系统为运行脚本程序创建 的shell。当一个...

AWK 技巧(取倒列,过滤行,匹配,不匹配,内置变量)

使用awk取某一行数据中的倒数第N列:$(NF-(n-1)) 比如取/etc/passwd文件中的第2列、倒数第1、倒数第2、倒数第4列(以冒号为分隔符)。($NF表示倒数第一列,$(NF-1)表示倒数第二列) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 [roo...

SQLServer数据库(二)

数据库设计:就是将数据库中的数据库实体及这些数据库实体之间的关系,进行规划和结构化的过程。 项目开发过程: 需求分析 概要设计 详细设计 代码编写 运行测试 打包发行 数据库的系统分析基本步骤:收集信息、标识实体、标识每个实体需要存储的详细信息、标识实体之间的关系。 实体,就是指现实世界中具有区分其它事物的特征或属性,并与其他实体有联系的实体。实体一般是名...

批处理脚本

常用dos命令 批处理复制文件(文件夹) 1.复制C:ae.txt 文本文件到d:文件夹下面【复制完的目录结构应该为d:e.txt】xcopy /y c:ae.txt d: >nul2.复制C:a文件夹及其内所有东西(包括隐藏,系统文件)到D:文件夹下面【 a文件夹也要复制过去,也就是说,复制之后,a文件夹应该在b文件夹下】xcopy /y /e...