- Home /
How to destroy a certain object with another certain object?
Hello unity! I'm a 10 year old developer currently working on an RPG. What I want to know is how do I make a pickaxe destroy rock. So I have a boulder in-front of my town and firstly I need to know how to destroy it on mouse click. Secondly I need to know how to destroy it with pickaxe only. Thanks!
Answer by aldonaletto · Jan 26, 2013 at 02:02 AM
Welcome to Unity! Well, destructible objects usually are completely fake (as almost everything in the game world): you may have several models, one for each destruction stage, and replace the rock model as the pickaxe destroys it; another possibility is to create models of the broken rock pieces, and join them together to represent the healthy rock - throw them out one by one after each pickaxe hit (or mouse click).
In both cases, however, the main work is to create the models, which must be done in some 3D editor like Blender, 3DMax, Maya etc.
EDITED: This is an example of the first technique, where there are different models for each level of destruction. You must make a model of the healthy rock, then duplicate this model and edit the copy to show some destruction, then duplicate it and add more destruction, and so on. Save all models and import them to Unity. Create an empty game object (Rock), drag the healty model to the scene, child it to the Rock and zero its position and rotation. Drag another model to the scene, child it to the Rock and adjust its position, then repeat this for all models. Drag the script below to the Rock, then in the Inspector assign the models to the array Models - the first one must be the healthy rock. The script has prevision for a pickaxe sound: if you want to use it, add an AudioSource component to the Rock. Finally, drag the Rock to the Project view to make it a prefab.
using UnityEngine;
using System.Collections;
public class DestructibleObject : MonoBehaviour {
public GameObject[] models; // drag the models orderly here
public AudioClip pickaxeSound; // add a sound, if you want
public float health = 100;
public float damage = 8;
private float curHealth = 0;
void SelModel(int num){
// disable all models first...
foreach (GameObject model in models){
model.renderer.enabled = false;
}
// then enable only the selected model
models[num].renderer.enabled = true;
}
void Update(){
if (health != curHealth){ // if health changed...
curHealth = health; // update curHealth...
// calculate model index: 0 is 100% healthy
int index = (int)(models.Length * (1 - (health / 100f)));
SelModel(index);
}
}
// Click the rock to damage it:
void OnMouseDown(){
if (pickaxeSound){ // if pickaxe sound defined, play it
audio.PlayOneShot(pickaxeSound);
}
health -= damage; // apply damage
if (health <= 0){ // if health ended, destroy rock
Destroy(gameObject);
}
}
}
Thank you! but may I have a script or something as I am learning C# so I would lovesome help. Thank you!
Your answer
Follow this Question
Related Questions
How To Make A Simple MMO? 3 Answers
A node in a childnode? 1 Answer
Pathfinding for RPG games 2 Answers
FPS KIT VERSION 2.0 0 Answers
Unity: C#: Eyes look at mouse: HELP!! 3 Answers