- 最后登录
- 2019-12-2
- 注册时间
- 2012-8-25
- 阅读权限
- 90
- 积分
- 34660
- 纳金币
- 38268
- 精华
- 111
|
找了很久都找不到相关的角色连招脚本,唯有自己苦想,功夫不负有心人,今天花了一个下午写了段角色连招,效果还不错,特分享一下。有需要的同学可以拿去使,大鸟可以顺便帮我修改下不足的地方······用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();
- }
- }
- }
复制代码 |
|