【重要】攻击动作时间段判断~使用动画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 ...
随机推荐
- 关于eclipse导工程或移植工程常碰到的错误汇总
在开发过程中,eclipse是使用得最多的IDE,但由于其开源且免费的性质决定了其不然有很多的BUG,在项目很赶的时期碰到某些很恶的错误很浪费时间,也很让人郁闷,现我总结一下我碰到的错误并总结下对 ...
- [转]jQuery选择器 (详解)
1).基本 #id 根据给定的ID匹配一个元素.例如:$("#id")element 根据给定的元素名匹配所有元素.例如:$("div").class 根据给定 ...
- linux网络配置练习
查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...
- Redis 缓存 + Spring 的集成示例(转载)
1. 依赖包安装 pom.xml 加入: <dependency> <groupId>org.springframework.data</groupId> < ...
- 浪漫程序员 HTML5爱心表白动画
我们程序员在追求爱情方面也是非常浪漫的,下面是一位同学利用自己所学的HTML5知识自制的HTML5爱心表白动画,画面非常温馨甜蜜,这样的创意很容易打动女孩,如果你是单身的程序员,也赶紧来制作自己的爱心 ...
- 消息中间件activemq-5.13.0整合spring
首先说明这里是在qctivemq配置好并启动服务的情况下进行,请先自行配置好.也可关注我的博文(消息中间件qctivemq安全验证配置)进行配置. 1.首先看一下项目结构 2.所需jar包,这里只列出 ...
- JAVA中的数字运算+号与字符串+号
(1)当“+”两边是非数值类型,“+”就被看作连接符. (2)当“+”两边都是数值类型,“+”就被看作算术运算中的加号. (3)当“+”一边是非数值,一边是数值类型,“+”就被看作连接符.
- 移植3.4.2的Kernel到JZ2440
本文将介绍如何移植linux-3.4.2内核到JZ2440开发板上的全过程,使用的交叉编译工具版本为 arm-linux-gcc-4.3.2.tar.bz2 下面来一步一步介绍如何移植 ...
- Robot Framework分层、开发系统关键字
开发系统关键字:http://www.cnblogs.com/fnng/p/4261293.html http://www.cnblogs.com/fnng/p/3969978.htm ...
- sar监控工具详解
转自http://www.cnblogs.com/Amaranthus/p/3745680.html SAR NAME: SAR报告,收集,保存系统活动信息 语法: sar [ -A ] [ -b ...