效果预览: (flash版本 文件3M,先等待一下) (webplayer版本 文件1.7M,先等待一下)
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() 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(); } }}