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);
    }
}
相关推荐
℡枫叶℡18 小时前
Unity - 全局配置Unity工程的资源检索的目录
unity·资源检索配置
mxwin18 小时前
Unity URP 下 TBN 矩阵学习 切线空间、tangent.w 与镜像 UV 的那些坑
学习·unity·矩阵·shader
程序猿多布18 小时前
Unity导表工具解决方案-Luban使用教程
unity·luban
mxwin18 小时前
Unity URP Shader 混合模式完全指南
unity·游戏引擎
mxwin19 小时前
Unity URP 下 HDR 与 Tonemapping 的 Shader 意识
unity·游戏引擎
沉默金鱼19 小时前
U3D高级编程:主程手记——第二章2.1读书笔记
unity·游戏引擎
mxwin1 天前
Unity Shader 深度写入与关闭ZWrite Off · 半透明排序 · 粒子穿插
unity·游戏引擎·shader
张老师带你学1 天前
宇宙飞船完整Unity项目
科技·游戏·unity·游戏引擎·模型
mxwin1 天前
Unity URP 下的流体模拟 深入解析 Navier-Stokes 方程与浅水方程的数学原理
unity·游戏引擎
mxwin1 天前
Unity Shader 深度重建世界坐标
unity·游戏引擎·shader