unity 2d 入门 飞翔小鸟 小鸟跳跃 碰撞停止挥动翅膀动画(十)

1、切换到动画器

点击make transition和exit关联起来

2、设置参数

勾选掉Has Exit Time

3、脚本给动画器传参

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Fly : MonoBehaviour
{
    //获取小鸟(刚体)
    private Rigidbody2D bird;
    //速度
    public float speed;
    //跳跃
    public float jump;
    //是否存活
    public static bool life = true;
    //获取动画器
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        bird = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //村换的时候才能运动
        if (life) { 
            bird.velocity = new Vector2(speed, bird.velocity.y);
            //鼠标点击给目标一个纵向速度
            if (Input.GetMouseButtonDown(0))
            {
                bird.velocity = new Vector2(bird.velocity.x, jump);
            }
        }
    }
    //如果碰撞器撞到了某个物体
    private void OnCollisionEnter2D(Collision2D collision)
    {
        life = false;
        animator.SetBool("life", false);
    }
}
相关推荐
变身缎带2 小时前
Unity里基于Luban的buff系统
数据库·unity·游戏引擎
变身缎带6 小时前
Unity中较为完善的NetworkManager
unity·游戏引擎
作孽就得先起床6 小时前
unity webGL导出.glb模型
unity·c#·游戏引擎·webgl
心前阳光9 小时前
Unity发布运行在PICO4的安卓程序
android·unity·游戏引擎
孟无岐9 小时前
【Laya】Scene3D 介绍
typescript·游戏引擎·游戏程序·laya
sunfove9 小时前
Python制作小游戏:用线性代数思想构建 2048 游戏引擎
python·线性代数·游戏引擎
孟无岐9 小时前
【Laya】Sprite3D 介绍
typescript·游戏引擎·游戏程序·laya
Howrun7779 小时前
虚幻引擎_创建组件
游戏引擎·虚幻
Jessica巨人9 小时前
pico相关插件介绍
unity
leo__52010 小时前
基于模糊控制的MATLAB避障算法实现
算法·matlab·unity