void Explode(Collision collision) { //add explosion force to surrounding objects Vector3 explosionPos = transform.position; int count = Physics.OverlapSphereNonAlloc(explosionPos, ExplosionRadius, overlapResults); for (int i = 0; i < count; i++) { Rigidbody otherRb = overlapResults[i].GetComponent <Rigidbody>(); if (otherRb != null) { Debug.Log("Apply force to " + overlapResults[i].gameObject.name); otherRb.AddExplosionForce(ExplosionForce * RigidbodyExplosionForceScale, explosionPos, ExplosionRadius, 0, ForceMode.Force); } LevelTerrain terrain = overlapResults[i].GetComponent <LevelTerrain>(); if (terrain != null) { Debug.Log("Apply force to " + overlapResults[i].gameObject.name); terrain.AddExplosionForce(ExplosionForce, explosionPos, this.rb.velocity.normalized, ExplosionRadius); } } //destroy Destroy(gameObject); }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Mouse0)) { //left mouse pressed Vector3 mousePos = Input.mousePosition; Ray ray = Camera.main.ScreenPointToRay(mousePos); if (Physics.Raycast(ray, out RaycastHit hitInfo)) { if (hitInfo.collider.GetComponent <LevelTerrain>() == terrain) { //hit terrain Debug.DrawLine(ray.origin, hitInfo.point, Color.green, 5); terrain.AddExplosionForce(ExplosionForce, hitInfo.point, ray.direction, ExplosionRadius); } } } }