Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

59 lines
1.7 KiB

namespace VRTK.Examples
{
using UnityEngine;
public class Breakable_Cube : MonoBehaviour
{
private float breakForce = 150f;
private void Start()
{
GetComponent<Rigidbody>().collisionDetectionMode = CollisionDetectionMode.Continuous;
}
private void OnCollisionEnter(Collision collision)
{
var collisionForce = GetCollisionForce(collision);
if (collisionForce > 0)
{
ExplodeCube(collisionForce);
}
}
private float GetCollisionForce(Collision collision)
{
if ((collision.collider.name.Contains("Sword") && collision.collider.GetComponent<Sword>().CollisionForce() > breakForce))
{
return collision.collider.GetComponent<Sword>().CollisionForce() * 1.2f;
}
if (collision.collider.name.Contains("Arrow"))
{
return 500f;
}
return 0f;
}
private void ExplodeCube(float force)
{
foreach (Transform face in GetComponentsInChildren<Transform>())
{
if (face.transform.name == transform.name) continue;
ExplodeFace(face, force);
}
Destroy(gameObject, 10f);
}
private void ExplodeFace(Transform face, float force)
{
face.transform.SetParent(null);
Rigidbody rb = face.gameObject.AddComponent<Rigidbody>();
rb.isKinematic = false;
rb.useGravity = true;
rb.AddExplosionForce(force, Vector3.zero, 0f);
Destroy(face.gameObject, 10f);
}
}
}