SHADE_Y3/Assets/Scripts/Gameplay/Item/SC_Breakable.cs

65 lines
1.4 KiB
C#

using SHADE;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
public class Breakable : Script
{
public float threshHold = 1.0f;
public float force = 2.0f;
private RigidBody rb;
private Transform trans;
private bool isBreak = false;
private List<GameObject> itemPieces = new List<GameObject>();
private Random ran = new Random();
protected override void awake()
{
rb = GetComponent<RigidBody>();
if (!rb)
Debug.LogError("RIGIDBODY EMPTY");
trans = GetComponent<Transform>();
if(!trans)
Debug.LogError("TRANSFORM EMPTY");
foreach (GameObject pieces in GameObject)
{
itemPieces.Add(pieces);
pieces.SetActive(false);
}
}
protected override void update()
{
if (isBreak)
Break();
}
protected override void onCollisionEnter(CollisionInfo info)
{
if (rb.LinearVelocity.GetSqrMagnitude() > threshHold)
{
isBreak = true;
}
}
protected override void onTriggerEnter(CollisionInfo info)
{
}
private void Break()
{
foreach (GameObject item in itemPieces)
{
item.SetActive(true);
item.GetComponent<Transform>().GlobalPosition = trans.LocalPosition + item.GetComponent<Transform>().LocalPosition;
GameObject gO = item;
gO.Parent = GameObject.Null;
}
isBreak = false;
Audio.PlaySFXOnce2D("event:/Props/impact_break");
Owner.SetActive(false);
}
}