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 Lehar17 小时前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎
Flamesky17 小时前
Unity编辑器重新编译代码
unity·重新编译
虾球xz20 小时前
游戏引擎学习第277天:稀疏实体系统
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第276天:调整身体动画
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第275天:将旋转和剪切传递给渲染器
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第268天:合并调试链表与分组
c++·学习·链表·游戏引擎
qq_5982117571 天前
Unity.UGUI DrawCall合批笔记
笔记·unity·游戏引擎
南玖yy2 天前
C/C++ 内存管理深度解析:从内存分布到实践应用(malloc和new,free和delete的对比与使用,定位 new )
c语言·开发语言·c++·笔记·后端·游戏引擎·课程设计
Tech Synapse2 天前
Unity ML-Agents实战指南:构建多技能游戏AI训练系统
人工智能·游戏·unity
虾球xz2 天前
游戏引擎学习第272天:显式移动转换
c++·学习·游戏引擎