栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > 其他

最简单Unity 连招入门--只有20行代码

其他 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

最简单Unity 连招入门--只有20行代码

https://www.bilibili.com/video/BV1EY4y1Y7Rq?spm_id_from=333.999.0.0

涉及 4个脚本,4段动画(Idle,Attack One,AttackTwo,AttackThree),三个trigger

三个trigger 命名为AttackOne,AttackTwo,AttackThree

AttackOne 是在  idle 到  AttackOne之间

AttackTwo 是在   Transition One 到  Attack Two之间

AttackThree 是在 Transition Two 到Attack three

注意 Transition one 是第一段攻击动画的后半段,Attack one 是整个动画的前半段,其实是一个动画分两段,需要从外面拖两次进来不同命名,调整时间。

同上,Transition Two 和 Attack Two同样原理

如下图所示

 

 

4个脚本

为如下,其中comboMainControl挂到你的角色上

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

public class comboMainControl : MonoBehaviour
{
    public static comboMainControl instance;

    public void Awake()
    {
        instance = this;
    }
    public bool inputRecieved;
   
    private void Update()
    {    
        Attack();
    }
    public void Attack()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            inputRecieved = true;
        }
        else
        {
            inputRecieved = false;
        }
    }
}

动画控制器里 单击Idle后 在右面add component 可以添加如下脚本

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

public class idleBehaviour : StateMachineBehaviour
{
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    //override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
   override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (comboMainControl.instance.inputRecieved)
        {
          //  Debug.Log("first trigger");
            animator.SetTrigger("AttackOne");
           
            
        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

动画控制器里 单击ITransition One后 在右面add component 可以添加如下脚本

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

public class transitionOne : StateMachineBehaviour
{
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
   // override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
  //  {
   //     
   // }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (comboMainControl.instance.inputRecieved)
        {
            animator.SetTrigger("AttackTwo");
            
          
        
        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

动画控制器里 单击ITransition Two后 在右面add component 可以添加如下脚本

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

public class transitionTwo : StateMachineBehaviour
{
    // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
   // override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
   // {
    
   // }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        if (comboMainControl.instance.inputRecieved)
        {
            animator.SetTrigger("AttackThree");
            
           
           

        }
    }

    // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    //override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    
    //}

    // OnStateMove is called right after Animator.OnAnimatorMove()
    //override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that processes and affects root motion
    //}

    // OnStateIK is called right after Animator.OnAnimatorIK()
    //override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    //{
    //    // Implement code that sets up animation IK (inverse kinematics)
    //}
}

一律无脑替换

你就可以流畅的出连招了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/901144.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号