https://github.com/dgreenheck/OpenFracture
GitHub - dgreenheck/OpenFracture: Open source mesh slicing/fracturing utility for Unity
Open source mesh slicing/fracturing utility for Unity - dgreenheck/OpenFracture
github.com
https://www.youtube.com/watch?v=s_v9JnTDCCY
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosion : MonoBehaviour {
public float cubeSize = 0.2f;
public int cubesInRow = 5;
float cubesPivotDistance;
Vector3 cubesPivot;
public float explosionForce = 50f;
public float explosionRadius = 4f;
public float explosionUpward = 0.4f;
// Use this for initialization
void Start() {
//calculate pivot distance
cubesPivotDistance = cubeSize * cubesInRow / 2;
//use this value to create pivot vector)
cubesPivot = new Vector3(cubesPivotDistance, cubesPivotDistance, cubesPivotDistance);
}
// Update is called once per frame
void Update() {
}
private void OnTriggerEnter(Collider other) {
if (other.gameObject.name == "Floor") {
explode();
}
}
public void explode() {
//make object disappear
gameObject.SetActive(false);
//loop 3 times to create 5x5x5 pieces in x,y,z coordinates
for (int x = 0; x < cubesInRow; x++) {
for (int y = 0; y < cubesInRow; y++) {
for (int z = 0; z < cubesInRow; z++) {
createPiece(x, y, z);
}
}
}
//get explosion position
Vector3 explosionPos = transform.position;
//get colliders in that position and radius
Collider[] colliders = Physics.OverlapSphere(explosionPos, explosionRadius);
//add explosion force to all colliders in that overlap sphere
foreach (Collider hit in colliders) {
//get rigidbody from collider object
Rigidbody rb = hit.GetComponent<Rigidbody>();
if (rb != null) {
//add explosion force to this body with given parameters
rb.AddExplosionForce(explosionForce, transform.position, explosionRadius, explosionUpward);
}
}
}
void createPiece(int x, int y, int z) {
//create piece
GameObject piece;
piece = GameObject.CreatePrimitive(PrimitiveType.Cube);
//set piece position and scale
piece.transform.position = transform.position + new Vector3(cubeSize * x, cubeSize * y, cubeSize * z) - cubesPivot;
piece.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
//add rigidbody and set mass
piece.AddComponent<Rigidbody>();
piece.GetComponent<Rigidbody>().mass = cubeSize;
}
}
https://www.youtube.com/watch?v=wcQqYOSteSs
https://www.youtube.com/watch?v=0SsJlS-Zsmg&t=435s
유니티 레이캐스트(Raycast) 확인하기 (0) | 2024.08.08 |
---|---|
유니티 시네머신 Freelock 카메라 (0) | 2024.08.07 |
콜라이더 충돌 태그로 처리하기 (1) | 2024.07.24 |
회전에서 Quaternion.Slerp(), Quaternion.RotateTowards()의 차이 (1) | 2024.07.24 |
유니티 캐릭터 컨트롤러 이동, 회전, 경사 오르기 (0) | 2024.07.24 |