Created
March 6, 2015 22:07
-
-
Save klutch/08eb2e7f5d220a09619e to your computer and use it in GitHub Desktop.
Misc Destructible Terrain Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections.Generic; | |
public class BaseDestructibleScript : MonoBehaviour | |
{ | |
public List<string> tags; | |
public PhysicsMaterial2D chunkPhysicsMaterial; | |
virtual public void onCollision(GameObject collisionObject) | |
{ | |
ShrinkAndDestroyScript shrinkAndDestroy = gameObject.AddComponent<ShrinkAndDestroyScript>(); | |
Vector2 velocity = collisionObject.rigidbody2D.velocity; | |
gameObject.layer = LayerMask.NameToLayer("Default"); | |
transform.parent = null; | |
rigidbody2D.isKinematic = false; | |
rigidbody2D.AddForce(velocity * 100f + Random.insideUnitCircle * 50f); | |
rigidbody2D.AddTorque(Random.Range(-1f, 1f) * 50f); | |
shrinkAndDestroy.time = 0.5f; | |
} | |
virtual public void copyFrom(BaseDestructibleScript source) | |
{ | |
tags = source.tags; | |
chunkPhysicsMaterial = source.chunkPhysicsMaterial; | |
collider2D.sharedMaterial = chunkPhysicsMaterial; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
public class TerrainDestructorScript : MonoBehaviour | |
{ | |
private int _mask; | |
private Collider2D[] _results; | |
public float radius = 1f; | |
void Start() | |
{ | |
_results = new Collider2D[255]; | |
_mask = LayerMask.GetMask("DestructibleTerrain"); | |
} | |
void FixedUpdate() | |
{ | |
int numResults = Physics2D.OverlapCircleNonAlloc(transform.position, radius, _results, _mask); | |
for (int i = 0; i < numResults; i++) | |
{ | |
BaseDestructibleScript destructibleScript = _results[i].gameObject.GetComponent<BaseDestructibleScript>(); | |
destructibleScript.onCollision(gameObject); | |
} | |
} | |
#if UNITY_EDITOR | |
void OnDrawGizmosSelected() | |
{ | |
Gizmos.color = Color.red; | |
Gizmos.DrawWireSphere(transform.position, radius); | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey. Can you explain how to use your script?