UNITY游戏开发入门-Animator

Animator是unity3D游戏开发用于动画制作,其中通过windows->animator可以调出U3D中Animator框,将完整的素材动画拖入,可以在该框中看到动画的各个组成部分。
在这里插入图片描述
在页面的右端点击其中一个动画后,可以修改该动画的名称等参数
在这里插入图片描述
每个部分动画都可以选中后点击鼠标右键Make Transition显示连接箭头,箭头意味从该被指向动画可以跳跃至指向动画。
附上一段枪械开火代码,仅供参考
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class weapon : MonoBehaviour
{
public int bulletTotal = 100; //子弹总数
public int bulletPermag = 30;//弹夹
public int bulletCurrent;//弹夹内子弹数量
public int damage = 20; //武器伤害属性

public float fireRate = 0.1f;//射击频率
Animator anim;//射击动画
public RaycastHit hit;//发射射线
float lastTime;//开枪最后时间

public AudioSource shootAudio;
public AudioClip gunShotFx;//发射子弹的声音
public AudioClip reloadFx; //换弹夹的声音
public AudioClip noBulletFx;//没有子弹的声音

public ParticleSystem muzzleFash;//发射子弹的火焰
public Transform shootPoint;//射击点
public float shootDistance = 200f;//射击距离

bool isMake = false;//当前动作是否为开枪预备动作
public GameObject aeWeapon;//开枪准备时的枪支
public GameObject beWeapon;//移动中的枪支

public Canvas game;//角色死亡后的相机

void Start()
{
    game.enabled = false;
    bulletCurrent = bulletPermag;//初始弹夹为满

    anim = GetComponent<Animator>();

    isMake = false;
    aeWeapon.SetActive(isMake);
    beWeapon.SetActive(!isMake);

}

// Update is called once per frame
void Update()
{
    if ((Input.GetKey(KeyCode.Q)))
    {
        isMake = !isMake;
        aeWeapon.SetActive(isMake);
        beWeapon.SetActive(!isMake);
    }

    if(isMake)
    {
        anim.Play("idle");
    }
    if(!isMake)
    {
        anim.Play("Grounded",0);
    }
    //控制开火
    if (Time.time - lastTime > fireRate)
    {
        if (Input.GetButton("Fire1")&&isMake)
        {
            Fire();
            lastTime = Time.time;
        }
    }

    //换子弹
    if (Input.GetKey(KeyCode.R) || (bulletCurrent == 0 && bulletTotal > 0)&&isMake)
    {
        Reload();
    }

    //角色死亡
    if (PlayerHealth.currentHealth <= 0)
    {
        Dead();
    }
}

//开火
void Fire()
{
    if (bulletCurrent > 0)
    {
        anim.Play("fire", 0);
        shootAudio.PlayOneShot(gunShotFx);
        muzzleFash.Play();
        --bulletCurrent;//减少子弹
        if (Physics.Raycast(shootPoint.position, shootPoint.forward, out hit, 100f))
        {
            Debug.Log(hit.transform.name + "found");
            if(hit.transform.name=="AI")
            {
                AIhit.currentHealth -= damage;
            }
        }
    }

    else
    {
        shootAudio.PlayOneShot(noBulletFx);
    }
}

//换子弹
void Reload()
{
    if (bulletTotal > 0)
    {
        int bulletNeed = bulletPermag - bulletCurrent;
        if (bulletNeed > 0)
        {
            anim.Play("reload");
            shootAudio.PlayOneShot(reloadFx);
            //判断当前所需要填满的子弹和总的剩余子弹数
            if (bulletNeed > bulletTotal)
            {
                bulletCurrent += bulletTotal;
                bulletTotal = 0;
            }
            else
            {
                bulletCurrent = 30;
                bulletTotal -= bulletNeed;
            }
        }
    }
}

//角色死亡
void Dead()
{
    anim.Play("playerDead");
    this.enabled = false;
    game.enabled = true;
}

}
更多详细用法,可以参考https://blog.csdn.net/qq_28849871/article/details/77914922或官方文档。

猜你喜欢

转载自blog.csdn.net/tfboys00/article/details/83006315