【重要】攻击动作时间段判断~使用动画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 ...
随机推荐
- Qt安装过程中: configure 时发生的经典出错信息之”Basic XLib functionality test failed!”(Z..z..) 之 MySQL support cannot be enabled due to functionality test!
整出错信息是在./configure阶段Basic XLib functionality test failed!You might need to modify the include and li ...
- hash模块 hashlib不可逆加密 和 base64算法加密解密
hashlib模块 用于加密相关的操作,代替md5模块和sha模块,主要提供SHA1,SHA224,SSHA256,SHA384,SHA512,MD5算法 直接看代码案例: ---------md5- ...
- python3 实现mysql数据库连接池
首先声明一下,这篇博客进行了通过自己的代码方式,加上这篇博客,最后总结出这段代码.参考博客连接:http://blog.csdn.net/zbc1090549839/article/details/5 ...
- 今日Q群:QQ群众群友反馈问题的归纳总结
今日Q群:QQ群群友反馈问题的归纳总结 今天Q群里还算比较活跃,归纳总结后主要有以下几类问题: 一.如何在Excel中按指定规则对有颜色的单元格进行过滤删选 具体的解决办法,请参照今天发布微信 ...
- ZooKeeper系列
Zookeeper系列(一) ZooKeeper系列(二) ZooKeeper系列(三) ZooKeeper系列(四)
- NPOIHelp 按固定模板导出和直接导出
完整代码如下 using System; using System.Collections.Generic; using System.Data; using System.Text; using N ...
- zabbix server端自动发现和zabbix agent端自动注册
一.zabbix自动发现 利用zabbix的discovery功能可以实现自动批量添加主机的功能. Zabbix自动发现实现自定义主机名: 通过自动发现添加的客户端主机的Host name 是以IP地 ...
- 7款效果惊人的HTML5/CSS3应用
今天是周末,我为大家收集7个比较经典的HTML5/CSS3应用,每一个都提供源代码,效果非常惊人. 1.CSS3/jQuery创意盒子动画菜单 作为前端开发者,各种各样的jQuery菜单见过不少,这款 ...
- thinkphp3.2 控制器导入模型
方法一: public function index(){ $Member = new MemberModel(); $money = $Member->Money(); print_r($mo ...
- [转载]Android 生成keystore,两种方式
Refer : http://blog.csdn.net/ms03001620/article/details/8490314 一.eclipse 中生成android keystore 建立任意一个 ...