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

1、切换到动画器

在这里插入图片描述
点击make transition和exit关联起来

2、设置参数

在这里插入图片描述
勾选掉Has Exit Time

3、脚本给动画器传参

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);
    }
}

最近更新

  1. TCP协议是安全的吗?

    2023-12-11 01:02:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-11 01:02:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-11 01:02:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-11 01:02:03       18 阅读

热门阅读

  1. 5.3 函数-递归与求阶乘

    2023-12-11 01:02:03       28 阅读
  2. 人工智能原理复习

    2023-12-11 01:02:03       35 阅读
  3. 第9节:Vue3 指令

    2023-12-11 01:02:03       35 阅读
  4. C#动态调用C++DLL中的函数

    2023-12-11 01:02:03       37 阅读
  5. ts中type和interface类型声明的区别

    2023-12-11 01:02:03       42 阅读
  6. harmonyOS学习笔记之状态修饰器@state,@prop,@link

    2023-12-11 01:02:03       32 阅读
  7. 排列游戏 --- 动态规划 --- 题解

    2023-12-11 01:02:03       38 阅读