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);
    }
}
相关推荐
Magnum Lehar12 分钟前
wpf3d游戏引擎ControlTemplate.xaml.cs文件实现
游戏引擎·wpf
留待舞人归16 分钟前
【Unity3D优化】优化多语言字体包大小
游戏·unity·游戏引擎·unity3d·优化
wsdchong之小马过河1 小时前
2025虚幻引擎一般用什么模型格式
游戏引擎·虚幻
Magnum Lehar7 小时前
wpf游戏引擎前端的Transform.cs实现
前端·游戏引擎·wpf
Magnum Lehar11 小时前
wpf3d游戏引擎前端ControlTemplate实现
前端·游戏引擎·wpf
97650333512 小时前
iOS 审核 cocos 4.3a【苹果机审的“分层阈值”设计】
flutter·游戏·unity·ios
EQ-雪梨蛋花汤1 天前
【Unity笔记】Unity Animation组件使用详解:Play方法重载与动画播放控制
笔记·unity·游戏引擎
AgilityBaby1 天前
Untiy打包安卓踩坑
android·笔记·学习·unity·游戏引擎
菌菌巧乐兹1 天前
Unity | AmplifyShaderEditor插件基础(第九集:旗子进阶版)
unity·游戏引擎
心前阳光1 天前
Unity编辑器-获取Projectwindow中拖拽内容的路径
unity·编辑器·游戏引擎