纳金网
标题:
Unity3D角色连招脚本
[打印本页]
作者:
狂风大尉
时间:
2014-6-26 20:05
标题:
Unity3D角色连招脚本
找了很久都找不到相关的角色连招脚本,唯有自己苦想,功夫不负有心人,今天花了一个下午写了段角色连招,效果还不错,特分享一下。有需要的同学可以拿去使,大鸟可以顺便帮我修改下不足的地方······用c#写的
using UnityEngine;
using System.Collections;
public class PlayerAttackController : MonoBehaviour {
//记录当前攻击动画;
AnimationClip currentAttack;
//获取人物动画组件;
Animation playerAnimation;
//动画标签;
public enum ActionState
{
Attack_0,
Attack_1,
None
}
//记录当前动画标签;
private ActionState actionState = ActionState.None;
/**
* 攻击的逻辑
*/
void AttackLogic(){
if (Input.GetButtonDown("Fire1")){
if (actionState != ActionState.Attack_0 && actionState != ActionState.Attack_1) {
actionState = ActionState.Attack_0;
}else if (actionState == ActionState.Attack_0 && playerAnimation[currentAttack.name].time > 1.0f){
actionState = ActionState.Attack_1;
}
}
}
/**
* 攻击套路选择
*/
void Attacks (){
float delayTime =0.0f;
switch (actionState){
case ActionState.Attack_0:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack", 0.15f);
currentAttack = playerAnimation["Attack"].clip;
break;
case ActionState.Attack_1:
delayTime = -0.1f;
playerAnimation.CrossFade("Attack01", 0.15f);
currentAttack = playerAnimation["Attack01"].clip;
break;
case ActionState.None:
break;
}
//Switch to default if an animation is almost over
if (playerAnimation[currentAttack.name].time > (playerAnimation[currentAttack.name].length +delayTime)){
actionState = ActionState.None;
currentAttack = playerAnimation["Idle"].clip;
}
}
void Awake(){
//程序开启前,获取人物动画组件;
playerAnimation = GetComponent <Animation>() as Animation;
}
void Start () {
if (playerAnimation.clip) {
currentAttack = playerAnimation.clip;
} else {
currentAttack = playerAnimation["Idle"].clip;
}
}
// Update is called once per frame
void Update () {
if (currentAttack != null){
AttackLogic();
Attacks();
}
}
}
复制代码
作者:
hyui
时间:
2014-6-26 21:21
感谢分享这个!
作者:
HIDEOKOJIMA
时间:
2014-6-26 22:40
感谢分享这个!
作者:
wucnj
时间:
2014-6-27 13:20
感谢分享!!!
欢迎光临 纳金网 (http://c-www.narkii.com/club/)
Powered by Discuz! X2.5