找回密码
 立即注册
楼主: yyj469367953

[脚本] 关于角色三连击

[复制链接]
发表于 2015-1-14 18:14 | 显示全部楼层 |阅读模式
今天在蛮牛教育学习群里(390835120)我们讨论了 一个关于三连击的实现方法
经过指点 有了想法决定 实验下 小白在这里也拿来跟大家分享下共同学习么
嗯 我们来拿这个壮汉实验一下吧
1
首先 我们要先研究下 他的动作关系 这个角色的动作有idle attack1 attack2  attack3 jump  run    那么我们要使用 unity 新动画系统的 Animator Controller 来进行 动画的连接
创建一个  Animator Controller 把咱用到的动画 都拖进去  吧 idle 设为默认动画
然后  咱就开始连连看吧 三连击击肯定是  idle--->attack1--->attack2--->attack3
这个套路   然后  这3个攻击到哪攻击一定时间不操作了  肯定 就会放回idle 状态
所以需要 这么连接  
2
连好后 我需要 拿脚本 来控制他们的切换  需要 一个变量  我们来建立一个attack int 变量 给那些线 添加这个变量   
  Idle attack1   Int 值为 1
  attack1attack2   Int 值为 2
  Attack2attack3   Int 值为 3
  attack1  attack2  attack3  返回 idle    int 都为 0
好了 这样 就可以拿这个变量控制啦
下面开始 写脚本
调用 动画  什么的就不说啦
直接说重点  三连击 核心就是 拿时间判断 你在这段时间内让没让我做出下一个动作
那么 怎么获取动画 时间之类的呢   
我们可以用 animSta=anim.GetCurrentAnimatorStateInfo (0);  来获取动画信息
这样 就可以 用 它里面 的is name  判断 现在 哪个动画名下面, 用 normalizedTime
来判断 动画 时间 剩下的 就是条件判断咯  
代码如下


using UnityEngine;
using System.Collections;

public class play_Move : MonoBehaviour {

        public float playSpeed=1;
        public float rotspeed=1;
        public Animator anim;
        public AnimatorStateInfo animSta;
        private const string IdleState="idle";  
        private const string Attack1State="attack1";  
        private const string Attack2State="attack2";  
        private const string Attack3State="attack3";
        private int HitCount=0;
        public float jumpli=10;
        void Start () {
                anim = this.gameObject.GetComponent<Animator> ();
          
                anim.SetBool ("move", false);
                anim.SetBool ("attack", false);
                anim.SetBool ("attackBack", false);
                anim.SetBool ("jump", false);
                HitCount=0;
        }
       

        void Update () {
                move ();
                animSta=anim.GetCurrentAnimatorStateInfo (0);
                if (!animSta.IsName (IdleState) && animSta.normalizedTime > 1.0f) {
                        anim.SetInteger("attack",0);
                         HitCount=0;
                        }

                if (Input.GetMouseButton (0)) {
                               
                        attack();
                }

                if (Input.GetKey (KeyCode.Space)) {
                                anim.SetBool ("jump", true);               
                                } else {
                           anim.SetBool ("jump",false);       
                }

        }

        void move(){

                float hor = Input.GetAxis ("Horizontal");
                float ver = Input.GetAxis ("Vertical");
                transform.Translate (0, 0, ver * playSpeed * Time.deltaTime);
                transform.Rotate (0, hor*rotspeed*Time.deltaTime, 0);
                if (ver != 0) {
                        anim.SetBool ("move", true);               
                } else {
                        anim.SetBool ("move", false);

                }
       
       
       
        }

        void attack(){
                if (animSta.IsName (IdleState) && HitCount == 0 && animSta.normalizedTime > 0.50f) {
                                                 anim.SetInteger ("attack", 1);
                                                 HitCount = 1;
                                     Debug.Log("cc");
                                } else
                if (animSta.IsName (Attack1State) && HitCount == 1 && animSta.normalizedTime > 0.65f) {
                                                anim.SetInteger ("attack", 2);               
                                                HitCount = 2;
                                } else
                if (animSta.IsName (Attack2State) && HitCount == 2 && animSta.normalizedTime > 0.70f) {
                                anim.SetInteger ("attack", 3);               
                                HitCount = 3;
               
                }

        }



}


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2015-4-4 10:19 | 显示全部楼层
赞一下,支持
发表于 2017-2-23 16:25 | 显示全部楼层

感谢楼主的无私分享!
发表于 2017-3-11 12:05 | 显示全部楼层
很不错
发表于 2017-3-11 12:41 | 显示全部楼层
顶顶多好
发表于 2017-3-11 11:53 | 显示全部楼层
真心顶
发表于 2017-3-11 12:06 | 显示全部楼层
难得一见的好帖
发表于 2017-3-11 12:08 | 显示全部楼层
说的非常好
发表于 2017-4-30 18:02 | 显示全部楼层
很不错
发表于 2017-4-30 18:39 | 显示全部楼层
楼主是超人
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-29 06:34 , Processed in 0.095013 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表