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);
    }
}
相关推荐
淡海水7 小时前
38-Hybrid生态-LeanCLR总览
unity·架构·c#·热更新·clr·hybrid·leanclr
郝学胜-神的一滴8 小时前
[简化版 GAMES 101] 计算机图形学 13:从光栅化到着色——赋予三维像素光影灵魂
c++·计算机视觉·unity·godot·图形渲染·opengl·unreal
晓13138 小时前
【Cocos Creator 3.x】篇——第五章 项目实战优化技术
前端·javascript·游戏引擎
fqkw68 小时前
unity 安装MCP +uvx
unity·游戏引擎
魔士于安1 天前
unity 音乐会场景 unity2022
游戏·unity·游戏引擎·贴图·模型
一线灵1 天前
Axmol 3.x 输入系统重构:从 Touch/Mouse 到统一 Pointer,再到现代 InputField
重构·游戏引擎
Zwarwolf1 天前
Godot零散知识点项目汇总
游戏引擎·godot
Mediary1 天前
Unity is running with Administrator privileges, which isnot supported...
unity
游乐码1 天前
Unity基础(十四)场景异步加载
unity·游戏引擎
mxwin1 天前
Unity Shader URP:法线在空间变换上的特殊性
unity·游戏引擎·shader