开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   0null0

Unity3D 脚本实现动画精细化

个人分类: U3D | 2017-12-6 22:16

动画的精细化判断

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HeroAnimatorCtrl : MonoBehaviour {

    public static HeroAnimatorCtrl instance;

    [SerializeField]
    Transform _handSword;
    [SerializeField]
    Transform _backSword;
    [SerializeField]
    Transform _sword;

    Animator _an;
    AnimatorStateInfo _currentState;
    AnimatorStateInfo _nextState;
    int attackCount = 0;
    private void Awake()
    {
        instance = this;
    }
    void Start () {
        _an = GetComponent<Animator>();
	}
	
	void Update () {
        bool hasAttack = false;
        _currentState = _an.GetCurrentAnimatorStateInfo(0);   //获取当前动画状态信息 参数为动画所在的层
        _nextState = _an.GetNextAnimatorStateInfo(0);      //获取下一个要执行动画状态的信息  参数同上
        if (Input.GetMouseButtonDown(0))
        {
            hasAttack = true;
        }
        if (hasAttack)
        {
            Attack();
        }
        if (_nextState.IsName("Base Layer.Idles.Idle")&&attackCount!=0&&_an.IsInTransition(0))
        {
            attackCount = 0;
            _an.SetInteger("Attack",attackCount=0);
        }
	}

    public void Attack()
    {
        if (_currentState.IsName("Base Layer.Idles.Idle")&&attackCount==0)
        {
            attackCount++;  //1
            _an.SetInteger("Attack",attackCount);
        }
        else if (_currentState.IsName("Base Layer.Attacks.Attack1")&&_currentState.normalizedTime>0.3&&attackCount==1)
        {
            attackCount++; //2
            _an.SetInteger("Attack", attackCount);
        }
        else if (_currentState.IsName("Base Layer.Attacks.Attack2") && _currentState.normalizedTime > 0.3 && attackCount == 2)
        {
            attackCount++; //3
            _an.SetInteger("Attack", attackCount);
        }
        else if (_currentState.IsName("Base Layer.Attacks.Attack3") && _currentState.normalizedTime > 0.3 && attackCount == 3)
        {
            attackCount++;  //4
            _an.SetInteger("Attack", attackCount);
        }
        else if (_currentState.IsName("Base Layer.Attacks.Attack4") && _currentState.normalizedTime > 0.3 && attackCount == 4)
        {
            attackCount++; //5
            _an.SetInteger("Attack", attackCount);
        }
    }


    public void HoldSword(int isHold)
    {
        if (isHold>0)
        {
            _sword.SetParent(_handSword,false);
        }
        else
        {
            _sword.SetParent(_backSword,false);
        }
    }

    public void AttackEvent(int attack)
    {
        if (attack>0)
        {
            Debug.Log("attack");
        }
        else
        {
            Debug.Log("stop");
        }
    }
}

0 0

作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

阅读排行

评论排行

推荐博客

最新博客

返回顶部