2. Unity2D horizontal frame animation sprite animation + animation state machine animator + silky combo action

ax general catalog

1. Creation of frame animation sprite animation

 2. Animation state machine animator

Teaching link https://blog.csdn.net/linxinfa/article/details/94392971?spm=1001.2014.3001.5506

Add an animator component to the character, create an animation controller, and drag it in.

Open the animation controller and drag the animation into the animation controller (only one controller will be added automatically). Then right click animation. Create connections to connect animations according to logical relationships. Then click "Parameters" on the upper left. Click on the connected line. Add some parameter conditions for animation transformation under the condition on the right. Parameters are controlled through scripts, animation transition conditions.

3. Combo action (four in a row)

Teaching link https://blog.csdn.net/weixin_30877755/article/details/95343666?spm=1001.2014.3001.5506

Layers and their parameters. Change the exit time of each link to 0, and the fixed time to 0

 Example of parameter conditions

 Why do you need to set the normalizedTime parameter after setting the attack? Because if you set a parameter, you will jump directly to the next action when you click the attack button. If you can't fully display the attack action, then add one more. Their parameters are used to determine which stage the behavior reaches, and the script is used to control their parameters to set normalizedTime to zero when entering the cause and exiting the behavior. In this way, you can click the attack button before going to the next action after the action is played.

 

 character script

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

public class playerControl : MonoBehaviour
{
    public bool Iscanjump = false;//是否能跳跃,默认不能
    private Rigidbody2D rig;//2D刚体

    float ScalX;//获取初始Scale.x,用于转向 
    public float dropConst;//下坠常数
    public float speed;//地面移动速度
    public float jumpspeedUp;//上升速度
    public float jumpspeedVertiacal;//空中左右移动速度

    private Animator ani;//动画控制器
    private AnimatorStateInfo state;//动画状态
                         // Start is called before the first frame update
    void Start()
    {
        rig = GetComponent<Rigidbody2D>();//获取刚体
        ani = GetComponent<Animator>();//获取动画控制器
        ScalX = transform.localScale.x;
    }

    // Update is called once per frame
    void Update()
    {
        move();//移动函数
        attack();//攻击函数-四连击
    }
    private void move()
    {    //水平,垂直俩个轴系
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        float dir = Input.GetAxisRaw("Horizontal");//方向-1 0 1
        //跳跃
        if (v > 0 && Iscanjump == true)
        {
            rig.velocity = new Vector2(0, jumpspeedUp);//设置刚体速度,给予向量
        }
        //长按高跳
        if (rig.velocity.y > 0 && Input.GetKey(KeyCode.W) && Iscanjump == false)
        {
            rig.velocity += Vector2.up * 0.2f;//长按高跳额外得到向上速度
        }
        //优化手感
        float a = dropConst * 5 - Mathf.Abs(rig.velocity.y);//通过下坠常数,空中速度快为0时,下坠常数a越大,即越快速 度过这个状态
        rig.velocity -= Vector2.up * a * Time.deltaTime;
        //方向改变
        if (dir != 0)
        {
            transform.localScale = new Vector3(dir* ScalX, transform.localScale.y, transform.localScale.z);//通过改变scale改变方向
        }
        //左右移动
        Vector3 vt = new Vector3(h, 0, 0).normalized;//vt为俩个轴系合成的方向向量,normalized单位化
            //移动动画
        if (dir != 0)
        {
            ani.SetBool("Ismove", true);
        }
        else { ani.SetBool("Ismove", false); }
   
        //空中左右移动,为地面jumpspeedVertiacal倍
        if (h != 0 && Iscanjump == false)
        {
            gameObject.transform.Translate(vt * speed * jumpspeedVertiacal * Time.deltaTime);//通过这个函数来使用vt使得左右移动
        }
        //地面左右移动
        else { gameObject.transform.Translate(vt * speed * Time.deltaTime); }
    }
    private void attack() {
        

        state = ani.GetCurrentAnimatorStateInfo(0);
        //判断播放完
        if ((state.IsName("attack1") || state.IsName("attack2") || state.IsName("attack3") || state.IsName("attack4")) && state.normalizedTime >= 1.0f)
        {
            
            ani.SetInteger("attack", 0);

        }

        if (Input.GetKey(KeyCode.J))
        {
    
            if (state.IsName("idle")||  state.IsName("move") && ani.GetInteger("attack")==0 )
            {
                ani.SetInteger("attack", 1);
            } else if (state.IsName("attack1")|| state.IsName("move") && ani.GetInteger("attack") == 1 )
            {
                ani.SetInteger("attack", 2);
            }
            else if (state.IsName("attack2")|| state.IsName("move") && ani.GetInteger("attack") == 2)
            {
                ani.SetInteger("attack", 3);
            }
            else if (state.IsName("attack3")|| state.IsName("move") && ani.GetInteger("attack") == 3)
            {
                ani.SetInteger("attack", 4);
            }
        }
        if (state.normalizedTime >=1.0f) 
        {
            ani.SetFloat("normalizedTime", state.normalizedTime);
        }

        if (state.normalizedTime>=1&& ani.GetInteger("attack")==0)
        {
            ani.SetLayerWeight(ani.GetLayerIndex("attack"), 0);
        }
        }
}

bottom script

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

public class IsJump : MonoBehaviour
{
    //为了优化跳跃碰撞盒,在父物体下面新建空对象bottoom,并添加碰撞盒,用来检测底部(以前碰撞盒头碰到顶部地面还能跳不符合常理,所以需要作此操作)
 
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
  
    }
    //检测否在地面碰撞盒子检测,通过给地面碰撞盒子transform的tag标签为ground
    private void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.transform.tag == "ground")
        {
 
            gameObject.GetComponentInParent<playerControl>().Iscanjump = true;//获取父物体脚本变量并赋值
            
        }

    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.transform.tag == "ground")
        {

            gameObject.GetComponentInParent<playerControl>().Iscanjump = false;//获取父物体脚本变量并赋值

        }
    }
}

Animation behavior script in state machine

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

public class SetNormalizedTime : StateMachineBehaviour
{
    private string normalizedTime = "normalizedTime";

    // 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)
    {
        animator.SetFloat(normalizedTime, 0);
    }

    // OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
    override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    {
        animator.SetFloat(normalizedTime, stateInfo.normalizedTime);
    }

    // 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)
    {
        animator.SetFloat(normalizedTime, 0);
    }
}

 

Next

3. Unity2D horizontal board smooth camera movement_ζั͡ ั͡fogั͡wolfั͡✾'s blog-CSDN blog Unity2D horizontal board smooth camera movement It's stiff, and it feels very responsive when turning. The following is my camera's silky optimization https://blog.csdn.net/qq_54263076/article/details/125640244

Guess you like

Origin blog.csdn.net/qq_54263076/article/details/125631721