54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
![]() |
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
|
|||
|
public class BreakAbleObstacle : Obstacle
|
|||
|
{
|
|||
|
|
|||
|
[Header("要破坏可破坏障碍物,需要多大的水平速度")]
|
|||
|
public float breakSpeedThreshold = 1f;
|
|||
|
|
|||
|
[Header("爆破碎片飞出时有多大力")]
|
|||
|
public float breakForce = 1f;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private Explodable explodable;
|
|||
|
private ExplosionForce explosionForce;
|
|||
|
|
|||
|
|
|||
|
|
|||
|
protected override void FindSth()
|
|||
|
{
|
|||
|
base.FindSth();
|
|||
|
explodable = transform.Find("Obstacle").GetComponent<Explodable>();
|
|||
|
explosionForce = transform.Find("Force").GetComponent<ExplosionForce>();
|
|||
|
}
|
|||
|
|
|||
|
void OnTriggerEnter2D(Collider2D other) {
|
|||
|
if(other.tag == "HorseHead")
|
|||
|
{
|
|||
|
if(other.transform.parent.tag != "Horse")
|
|||
|
Debug.LogWarning("用于检查可破碎障碍物的触发器捕获到了马头,但是通过马头没有找到马,很可能是马的树状结构或者tag的赋予上出了问题");
|
|||
|
CheckHorseSpeed(other.transform.parent);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void CheckHorseSpeed(Transform horse)
|
|||
|
{
|
|||
|
if(horse.GetComponent<Rigidbody2D>().velocity.x > breakSpeedThreshold)
|
|||
|
BreakObstacle(horse);
|
|||
|
}
|
|||
|
|
|||
|
void BreakObstacle(Transform horse)
|
|||
|
{
|
|||
|
explodable.explode();
|
|||
|
float force = horse.GetComponent<Rigidbody2D>().velocity.x * breakForce + 1;
|
|||
|
explosionForce.force = force;
|
|||
|
explosionForce.doExplosion(explosionForce.transform.position);
|
|||
|
GetComponent<BoxCollider2D>().enabled = false;
|
|||
|
}
|
|||
|
}
|