【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟

using UnityEngine;

using System.Linq;
using System.Collections.Generic;

[RequireComponent(typeof(CharacterMotor))]
[RequireComponent(typeof(CharacterStatus))]
[RequireComponent(typeof(CharacterAttack))]
[RequireComponent(typeof(CharacterInventory))]

public class CharacterSystem : MonoBehaviour
{

    public float Speed    = 2; // Move speed
    public float SpeedAttack = 1.5f; // Attack speed
    public float TurnSpeed    = 5; // turning speed

    public float[] PoseAttackTime;// list of time damage marking using to sync with attack animation
    public string[] PoseAttackNames;// list of attack animation
    public string[] ComboAttackLists;// list of combo set

    public string[] PoseHitNames;// pose animation when character got hit
    public int WeaponType; // type of attacking
    public string PoseIdle = "Idle";
    public string PoseRun = "Run";
    public bool IsHero;

    //private variable
    private bool diddamaged;
    private int attackStep = 0;
    private string[] comboList;
    private int attackStack;
    private float attackStackTimeTemp;
    private float frozetime;
    private bool hited;
    private bool attacking;

    CharacterMotor motor;

    void Start()
    {
        motor = gameObject.GetComponent<CharacterMotor>();
        // Play pose Idle first
        gameObject.animation.CrossFade(PoseIdle);
        attacking = false;
    }

    void Update()
    {
        // Animation combo system

        if(ComboAttackLists.Length<=0){// if have no combo list
            return;
        }

        comboList = ComboAttackLists[WeaponType].Split(","[0]);// Get list of animation index from combolists split by WeaponType

        if(comboList.Length > attackStep){
            int poseIndex = int.Parse(comboList[attackStep]);// Read index of current animation from combo array
            if(poseIndex < PoseAttackNames.Length){
                // checking index of PoseAttackNames list

                AnimationState attackState = this.gameObject.animation[PoseAttackNames[poseIndex]]; // get animation PoseAttackNames[poseIndex]
                attackState.layer = 2;
                attackState.blendMode = AnimationBlendMode.Blend;
                attackState.speed = SpeedAttack;

                if(attackState.time >= attackState.length * 0.1f){
                    // set attacking to True when time of attack animation is running to 10% of animation
                      attacking = true;
                  }
                   if(attackState.time >= PoseAttackTime[poseIndex]){
                    // if the time of attack animation is running to marking point (PoseAttackTime[poseIndex])
                    // calling CharacterAttack.cs to push a damage out
                      if(!diddamaged){
                        // push a damage out
                         this.gameObject.GetComponent<CharacterAttack>().DoDamage();
                      }
                }

                if(attackState.time >= attackState.length * 0.8f){
                    // if the time of attack animation is running to 80% of animation. It‘s should be Finish this pose.

                    attackState.normalizedTime = attackState.length;
                    diddamaged = true;
                    attacking = false;
                    attackStep += 1;

                    if(attackStack>1){
                        // checking if a calling attacking is stacked
                        fightAnimation();
                    }else{
                        if(attackStep>=comboList.Length){
                            // finish combo and reset to idle pose
                            resetCombo();
                              this.gameObject.animation.Play(PoseIdle);
                        }
                    }
                    // reset character damage system
                    this.gameObject.GetComponent<CharacterAttack>().StartDamage();
                  }
            }
        }

        if(hited){// Freeze when got hit
            if(frozetime>0){
                frozetime--;
            }else{
                hited = false;
                this.gameObject.animation.Play(PoseIdle);
            }
        }

        if(Time.time > attackStackTimeTemp+2){
            resetCombo();
        }

    }

    public void GotHit(float time){
        if(!IsHero){
            if(PoseHitNames.Length>0){
                // play random Hit animation
                this.gameObject.animation.Play(PoseHitNames[Random.Range(0,PoseHitNames.Length)], PlayMode.StopAll);
            }
            frozetime = time * Time.deltaTime;// froze time when got hit
            hited = true;
        }
    }

    private void resetCombo(){
        attackStep = 0;
        attackStack = 0;

    }

    private void fightAnimation(){

        attacking = false;
        if(attackStep>=comboList.Length){
              resetCombo();
        }

        int poseIndex = int.Parse(comboList[attackStep]);
        if(poseIndex < PoseAttackNames.Length){// checking poseIndex is must in the PoseAttackNames list.
            if(this.gameObject.GetComponent<CharacterAttack>()){
                // Play Attack Animation
                this.gameObject.animation.Play(PoseAttackNames[poseIndex],PlayMode.StopAll);
            }
            diddamaged = false;
        }
    }

    public void Attack()
    {
        if(frozetime<=0){
            attackStackTimeTemp = Time.time;
            fightAnimation();
            attackStack+=1;
        }

    }

    public void Move(Vector3 dir){
        if(!attacking){
            moveDirection = dir;
        }else{
            moveDirection = dir/2f;
        }
    }

    Vector3 direction;

    private Vector3 moveDirection
    {
        get { return direction; }
        set
        {
            direction = value;
            if(direction.magnitude > 0.1f)
            {
                var newRotation    = Quaternion.LookRotation(direction);
                transform.rotation    = Quaternion.Slerp(transform.rotation,newRotation,Time.deltaTime * TurnSpeed);
            }
            direction *= Speed * 0.5f * (Vector3.Dot(gameObject.transform.forward,direction) + 1);

            if(direction.magnitude > 0.001f)
            {
                // Play Runing Animation when moving
                float speedaimation = direction.magnitude * 3;
                gameObject.animation.CrossFade(PoseRun);
                if(speedaimation<1){
                    speedaimation = 1;
                }
                // Speed animation sync to Move Speed
                gameObject.animation[PoseRun].speed    = speedaimation;

            }
            else{
                // Play Idle Animation when stoped
                gameObject.animation.CrossFade(PoseIdle);
            }
            if(motor){
                motor.inputMoveDirection = direction;
            }
        }
    }

    float pushPower = 2.0f;
    void OnControllerColliderHit(ControllerColliderHit hit)// Character can push an object.
    {
        var body = hit.collider.attachedRigidbody;
        if(body == null || body.isKinematic){
            return;
        }
        if(hit.moveDirection.y < -0.3){
            return;
        }

        var pushDir = Vector3.Scale(hit.moveDirection,new Vector3(1,0,1));
        body.velocity = pushDir * pushPower;
    }

}

这段代码解决了我写配置表来控制动画攻击的问题 不需要用Time.time的变量来判断了

【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟

时间: 2024-10-06 10:40:58

【重要】攻击动作时间段判断~使用动画time比较动画length和使用一个变量数组做延迟的相关文章

如何做好游戏中的攻击动作-进阶教程

转自:http://www.gamelook.com.cn/2015/05/214071 GameLook报道/ 之前GameLook发布的<简单做好游戏中“攻击动作”的3个窍门>一文受到不少同学的欢迎,今天我们在上一篇文章的基础上对游戏中人物角色攻击动作的本质——身体运动的原理进行介绍,希望对大家今后的开发工作能够有所帮助. 今天我们从关节和骨骼肌的运动.作用与反作用法则.反作用动作.旋转速度的控制.运动连锁原则,这5个方面来详细介绍人物角色攻击动作制作中的注意点. 原文来自万代南梦宫工作室

cocos2dx游戏--欢欢英雄传说--为敌人添加移动和攻击动作

这里主要为敌人添加了一个移动动作和攻击动作.移动动作是很简略的我动他也动的方式.攻击动作是很简单的我打他也打的方式.效果:代码: #ifndef __Progress__ #define __Progress__ #include "cocos2d.h" USING_NS_CC; class Progress : public Sprite { public: bool init(const char* background, const char* fillname); /* the

hlg1306再遇攻击--射线法判断点是否在多边形内部

再遇攻击 Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 313(40 users) Total Accepted: 91(32 users) Rating:  Special Judge: No Description Dota中英雄技能攻击会有一个范围,现在释放一个技能给出他的攻击范围和目标英雄的位置,问是否能攻击到.攻击范围保证是一个多边型. Input 有多组测试数据 第一行输入1个整数n, 期中n代表攻击范围是给出的n个点

原生js判断css动画结束 css 动画结束的回调函数

原文:原生js判断css动画结束 css 动画结束的回调函数 css3 的时代,css3--动画 一切皆有可能: 传统的js 可以通过回调函数判断动画是否结束:即使是采用CSS技术生成动画效果,JavaScript仍然能捕获动画或变换的结束事件: transitionend事件和animationend事件标准的浏览器事件,但在WebKit浏览器里你仍然需要使用webkit前缀,所以,我们不得不根据各种浏览器分别检测事件 var transitions = { 'transition':'tra

锋利的jQuery-4--停止动画和判断是否处于动画状态(防止动画加入队列过多的办法)

1.停止元素的动画:stop([cleanQueue, gotoEnd]):第一个参数代表是否要清空未执行完的动画队列,第二个参数代表是否直接将正在执行的动画跳转到末状态. 无参数stop():立即停止当前的动画,如果接下来还有动画则以当前状态开始接下来的动画. 举例:为元素绑定hover事件之后,如果光标移入移出的速度太快,导致移入的动画还没执行完,就移出光标,则移出的动画效果就会被放到队列,等移入的动画完成后在执行.因此如果光标的移入移出速度太快,就会导致动画效果与移动光标不一致的情况. 此

iOS核心动画以及UIView动画的介绍

我们看到很多App带有绚丽狂拽的特效,别出心裁的控件设计,很大程度上提高了用户体验,在增加了实用性的同时,也赋予了app无限的生命力.这些华丽的效果很多都是基于iOS的核心动画原理实现的,本文介绍一些iOS开发中最基本的动画效果实现,掌握了基本属性,才可以绘制出更华丽的效果. 一.概念扩充  1.核心动画: Core Animation,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍. Core Animation可以用在Mac OS X和iOS平台.在iO

JS动画和CSS动画

一. JS动画和CSS动画区别. CSS实现动画:animation transition transform JS实现动画:setInterval  setTimeout  requestAnimationFrame JS动画: 优点: 1. 过程控制,可以在动画播放过程中对动画进行控制:开始.暂停.回放.终止.取消都是可以做到的. 2. 动画效果比css3动画丰富,有些动画效果,比如曲线运动,冲击闪烁,视差滚动效果,只有JavaScript动画才能完成. 3. CSS3有兼容性问题,而JS大

iOS:核心动画之关键帧动画CAKeyframeAnimation

CAKeyframeAnimation——关键帧动画 关键帧动画,也是CAPropertyAnimation的子类,与CABasicAnimation的区别是: –CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),而CAKeyframeAnimation会使用一个NSArray保存这些数值 – 属性说明: –values:上述的NSArray对象.里面的元素称为“关键帧”(keyframe).动画对象会在指定的时间(duration)内,依次显

iOS 自定义页面的切换动画与交互动画

在iOS7之前,开发者为了寻求自定义Navigation Controller的Push/Pop动画,只能受限于子类化一个UINavigationController,或是用自定义的动画去覆盖它.但是随着iOS7的到来,Apple针对开发者推出了新的工具,以更灵活地方式管理UIViewController切换. 自定义导航栏的Push/Pop动画 为了在基于UINavigationController下做自定义的动画切换,先建立一个简单的工程,这个工程的rootViewController是一个