【unity基础_Day11】 动画系统

一.动画系统

1.AnimatorController  动画控制器

(1)控制任务移动脚本

float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Vertical");
        float mouse_x = Input.GetAxis("Mouse X");
        if (Input.GetMouseButtonDown(0))
        {
            ani.SetBool(Attack1Id, true);
        }
        if (Input.GetMouseButtonUp(0))
        {
            ani.SetBool(Attack1Id, false);
        }
        if (x != 0 || y != 0 || mouse_x != 0)
        {
            ani.SetBool(WalkId, true);
            transform.Translate(x * Time.deltaTime*movsSpeed, 0, y * Time.deltaTime*movsSpeed);
            transform.Rotate(0, mouse_x*Time.deltaTime*1000, 0);
           
            if (Input.GetKeyDown(KeyCode.LeftShift))
            {
                ani.SetBool(RunId, true);
                transform.Translate(x * Time.deltaTime * movsSpeed * 2, 0, y * Time.deltaTime * movsSpeed * 4);
                transform.Rotate(0, mouse_x * Time.deltaTime, 0);
            }
            if (Input.GetKeyUp(KeyCode.LeftShift))
            {
                ani.SetBool(RunId, false);
            }
        
        }
        else
        {
            ani.SetBool(WalkId, false);
        }

(2)一般给模型创建父的空物体,在空物体上添加脚本操作

(3)事件的使用

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

public class AnimatorEventBehavoir : MonoBehaviour {

    public event Action attackHander;
    Animator ani;
	// Use this for initialization
	void Start () {
        ani=this.GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {

       
    }
    public void OnAttack()  
    {
        if (attackHander != null)
        {
            attackHander();
            //引发事件  脚本内容不再这个里面写,可以通过其他的脚本中进行写入

        }
    }

    /// <summary>
    /// 默认状态下 设置为 Idle 状态
    /// </summary>
    /// <param name="animParam">形参</param>
    void OnCancelAnim(string animParam)
    {
        ani.SetBool(animParam, false);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControllerTest : MonoBehaviour {

    AnimatorEventBehavoir animatorEvent;
    Animator aniTemp;
	// Use this for initialization
	void Start () {
        //获取子物体身上的组件
        animatorEvent = GetComponentInChildren<AnimatorEventBehavoir>();

        //注册事件
        animatorEvent.attackHander += OnAttackFunc;

        //获取子物体模型上的animator组件
        aniTemp = GetComponentInChildren<Animator>();
	}
	
	// Update is called once per frame
	 void Update () {
        if (Input.GetMouseButtonDown(0))
        {
            aniTemp.SetBool("IsAttack", true);
        }
	 }
    
    /// <summary>
    /// 具体的方法在这个里面写就可以了
    /// </summary>
   void OnAttackFunc()
    {
        print("已经注册了事件");
    }
}

版权声明:本文为weixin_45344894原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。