unity 2d 入门 飞翔小鸟 小鸟碰撞 及死亡(九)
1、给地面,柱体这种添加2d盒装碰撞器,小鸟移动碰到就不会动了
2、修改小鸟的脚本(脚本命名不规范,不要在意)
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;
//向动画器传送为life的参数
animator.SetBool("life", false);
}
}
小鸟挥动翅膀动画停止
见下一个章节