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 updatevoid Start(){bird = GetComponent<Rigidbody2D>();animator = GetComponent<Animator>();}// Update is called once per framevoid 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);}
}
小鳥揮動翅膀動畫停止
見下一個章節