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 = ; // Move speed
public float SpeedAttack = 1.5f; // Attack speed
public float TurnSpeed = ; // 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 = ;
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<=){// if have no combo list
return;
} comboList = ComboAttackLists[WeaponType].Split(","[]);// 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 = ;
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 += ; if(attackStack>){
// 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>){
frozetime--;
}else{
hited = false;
this.gameObject.animation.Play(PoseIdle);
}
} if(Time.time > attackStackTimeTemp+){
resetCombo();
} } public void GotHit(float time){
if(!IsHero){
if(PoseHitNames.Length>){
// play random Hit animation
this.gameObject.animation.Play(PoseHitNames[Random.Range(,PoseHitNames.Length)], PlayMode.StopAll);
}
frozetime = time * Time.deltaTime;// froze time when got hit
hited = true;
}
} private void resetCombo(){
attackStep = ;
attackStack = ; } 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<=){
attackStackTimeTemp = Time.time;
fightAnimation();
attackStack+=;
} } 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) + ); if(direction.magnitude > 0.001f)
{
// Play Runing Animation when moving
float speedaimation = direction.magnitude * ;
gameObject.animation.CrossFade(PoseRun);
if(speedaimation<){
speedaimation = ;
}
// 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(,,));
body.velocity = pushDir * pushPower;
} }

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

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

  1. ios基础动画、关键帧动画、动画组、转场动画等

    概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...

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

    1.停止元素的动画:stop([cleanQueue, gotoEnd]):第一个参数代表是否要清空未执行完的动画队列,第二个参数代表是否直接将正在执行的动画跳转到末状态. 无参数stop():立即停 ...

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

    原文:原生js判断css动画结束 css 动画结束的回调函数 css3 的时代,css3--动画 一切皆有可能: 传统的js 可以通过回调函数判断动画是否结束:即使是采用CSS技术生成动画效果,Jav ...

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

    这里主要为敌人添加了一个移动动作和攻击动作.移动动作是很简略的我动他也动的方式.攻击动作是很简单的我打他也打的方式.效果:代码: #ifndef __Progress__ #define __Prog ...

  5. Unity3D Mecanim 动画系统骨骼动画问题解决方法

    http://7dot9.com/2014/08/16/unity3d-mecanim%E5%8A%A8%E7%94%BB%E7%B3%BB%E7%BB%9F%E9%AA%A8%E9%AA%BC%E5 ...

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

    我们看到很多App带有绚丽狂拽的特效,别出心裁的控件设计,很大程度上提高了用户体验,在增加了实用性的同时,也赋予了app无限的生命力.这些华丽的效果很多都是基于iOS的核心动画原理实现的,本文介绍一些 ...

  7. Android 动画基础——视图动画(View Animation)

    本篇讲android 3.0之前被广泛的动画框架——ViewAnimation. 目录 我将分为六部分来讲: 概述 Alpha透明动画 Rotate旋转动画 Translate位移动画 Scale放缩 ...

  8. 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画

    CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...

  9. Android动画:模拟开关按钮点击打开动画(属性动画之平移动画)

    在Android里面,一些炫酷的动画确实是很吸引人的地方,让然看了就赏心悦目,一个好看的动画可能会提高用户对软件的使用率.另外说到动画,在Android里面支持3种动画: 逐帧动画(Frame Ani ...

随机推荐

  1. 重点:QObject 的拷贝构造和赋值操作——私有

    QObject 中没有提供一个拷贝构造函数和赋值操作符给外界使用,其实拷贝构造和赋值的操作都是已经声明了的,但是它们被使用了Q_DISABLE_COPY () 宏放在了private区域.因此所有继承 ...

  2. 关于socket的知识总结

    简单点说: 阻塞就是干不完不准回来,    非组赛就是你先干,我现看看有其他事没有,完了告诉我一声 我们拿最常用的send和recv两个函数来说吧... 比如你调用send函数发送一定的Byte,在系 ...

  3. C语言中的运算和运算符

    一.运算符的优先级和结合性 1,优先级 运算符一览表中,运算符越靠上,优先级越高. 2,结合性 假如用O表示需要两个操作数的双目运算符,那么对于表达式aObOc: 左结合运算符会将表达式解释为  (a ...

  4. Mac 创建证书(以 创建gdb证书 为例 )

    open /Applications/Utilities/Keychain\ Access.app/ 打开 钥匙串访问 继续继续 创建完毕. Now that we have a certificat ...

  5. 使 Finder 显示 文件夹路径

    显示路径: cd ~ defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE killall Finder 不显示路径: ...

  6. TCP/IP,http,socket,长连接,短连接——小结(转)

    概要: 之前对这几个概念有点糊涂,查阅了些资料,稍微概括下他们的区别吧.如有错误,请拍~~~ 先看图: TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层.    在 ...

  7. WebService系列一:WebService简介

    原文链接:http://www.cnblogs.com/xdp-gacl/p/4259109.html 一.WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技 ...

  8. JDBC WHERE子句条件实例

    在本教程将演示如何在JDBC应用程序中,从数据库表中查询数据记录, 在查询选择记录时使用WHERE子句添加其他条件. 在执行以下示例之前,请确保您已经准备好以下操作: 具有数据库管理员权限,以在给定模 ...

  9. Android Jsoup 爬取网页数据

    一不小心一个月又过去了,事实上近期还是小忙小忙的,废话不多说.直接进入今天的主题吧. Jsoup – Java HTML Parser, with best of DOM, CSS, and jque ...

  10. 【转】IOS 学习之 NSPredicate 模糊、精确、查询

    转自:http://blog.csdn.net/lianbaixue/article/details/10579117   简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于S ...