【重要】攻击动作时间段判断~使用动画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 = ; // 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和使用一个变量数组做延迟的更多相关文章
- ios基础动画、关键帧动画、动画组、转场动画等
概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建基础动画.关键帧动画 ...
- 锋利的jQuery-4--停止动画和判断是否处于动画状态(防止动画加入队列过多的办法)
1.停止元素的动画:stop([cleanQueue, gotoEnd]):第一个参数代表是否要清空未执行完的动画队列,第二个参数代表是否直接将正在执行的动画跳转到末状态. 无参数stop():立即停 ...
- 原生js判断css动画结束 css 动画结束的回调函数
原文:原生js判断css动画结束 css 动画结束的回调函数 css3 的时代,css3--动画 一切皆有可能: 传统的js 可以通过回调函数判断动画是否结束:即使是采用CSS技术生成动画效果,Jav ...
- cocos2dx游戏--欢欢英雄传说--为敌人添加移动和攻击动作
这里主要为敌人添加了一个移动动作和攻击动作.移动动作是很简略的我动他也动的方式.攻击动作是很简单的我打他也打的方式.效果:代码: #ifndef __Progress__ #define __Prog ...
- 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 ...
- iOS核心动画以及UIView动画的介绍
我们看到很多App带有绚丽狂拽的特效,别出心裁的控件设计,很大程度上提高了用户体验,在增加了实用性的同时,也赋予了app无限的生命力.这些华丽的效果很多都是基于iOS的核心动画原理实现的,本文介绍一些 ...
- Android 动画基础——视图动画(View Animation)
本篇讲android 3.0之前被广泛的动画框架——ViewAnimation. 目录 我将分为六部分来讲: 概述 Alpha透明动画 Rotate旋转动画 Translate位移动画 Scale放缩 ...
- 梅须逊雪三分白,雪却输梅一段香——CSS动画与JavaScript动画
CSS动画并不是绝对比JavaScript动画性能更优越,开源动画库Velocity.js等就展现了强劲的性能. 一.两者的主要区别 先开门见山的说说两者之间的区别. 1)CSS动画: 基于CSS的动 ...
- Android动画:模拟开关按钮点击打开动画(属性动画之平移动画)
在Android里面,一些炫酷的动画确实是很吸引人的地方,让然看了就赏心悦目,一个好看的动画可能会提高用户对软件的使用率.另外说到动画,在Android里面支持3种动画: 逐帧动画(Frame Ani ...
随机推荐
- Writing a Reusable Custom Control in WPF
In my previous post, I have already defined how you can inherit from an existing control and define ...
- 实现接口时@Override注解问题
用IntelliJ 15打开一个以前的工程,发现代码出现很多关于@Override的错误,编辑器提示:“@Override is not allowed when implementing int ...
- 技能UP:SAP OBYC自动记账的实例说明(含value String应用说明)
一. 自动过账原理 在MM模块的许多操作都能实现在FI模块自动过账,如PO收货.发票验证(LIV).工单发料.向生产车间发料等等.不用说,一定需要在IMG中进行配置才可以实现自动处理.但SAP实现的这 ...
- 【转】【Python】Python3爬虫实现自动登录、签到
工具:Fiddler 首先下载安装Fiddler,这个工具是用来监听网络请求,有助于你分析请求链接和参数. 打开目标网站:http://www.17sucai.com/,然后点击登录 好了,先别急着登 ...
- Android Error: This attribute must be localized.
在android中使用mmm命令编译程序是出现错误. 这种问题一般情况是因为在res/xml文件夹下的中, 或者在res/layout下的文件中出现了没有多语言话的文本例. 解决方法: 不直接在布局文 ...
- Lucene系列二:Lucene(Lucene介绍、Lucene架构、Lucene集成)
一.Lucene介绍 1. Lucene简介 最受欢迎的java开源全文搜索引擎开发工具包.提供了完整的查询引擎和索引引擎,部分文本分词引擎(英文与德文两种西方语言).Lucene的目的是为软件开发人 ...
- Spring JDBC批量操作
以下示例将演示如何使用spring jdbc进行批量更新.我们将在单次批次操作中更新student表中的记录. student表的结果如下 - CREATE TABLE student( id INT ...
- memcached -- 运行memcached
Memcached 运行 Memcached命令的运行: $ /usr/local/memcached/bin/memcached -h 注意:如果使用自动安装, memcached 命令位于 /us ...
- js作为参数,并且返回值;js的回调模式 callback
有这样一个情景,当我们弹出一个 prompt的时候,要求用户在文本框输入一个文字,然后点击确认,就可以拿到返回值 var temp=prompt("请输入您的名字"); a ...
- DbHelper.ttinclude 更新,查询视图和表
<#+ public class DbHelper { #region GetDbTables public static List<DbTable> GetDbTables(str ...