Hi all
I have a cannon that need (or want :D) shoots cannonballs on a castle…
How I can make the castle destroy?
I need to put a crack in hit point and when it is hitten X times, I want to broke the piece of castle in half part and make it fall down…
I know, it’s not easy 
Thank you for attention
I have written a little demonstration to show how it can be done by another method.
Create a cube, name it Floor. Set the Transform in the Inspector :
// Position : X = 0; Y = -0.5; Z = 20;
// Rotation: X = 0; Y = 0; Z = 0;
// Scale: X = 50; Y = 1; Z = 50;
Give it a material !
Now, Create a cube, name it Wall. Set the Transform in the Inspector :
// Position : X = 0; Y = 2.5; Z = 5.5;
// Rotation: X = 0; Y = 0; Z = 0;
// Scale: X = 5; Y = 7; Z = 1;
Apply a material !
On the Wall cube, Set the Collider Is Trigger to true (tick the box).
For a good result, set the Size of the Box Collider to 0.95 for all X Y and Z.
Now attach this script :
#pragma strict
import System.Collections.Generic;
var rubble : List.< GameObject > = new List.< GameObject >();
var myTransform : Transform;
function Start()
{
myTransform = transform;
BuildBricks();
}
function BuildBricks()
{
// how wide/tall/thick is the wall
var myScale : Vector3 = Vector3( parseInt( myTransform.localScale.x ), parseInt( myTransform.localScale.y ), parseInt( myTransform.localScale.z ) );
//Debug.Log( myScale );
// loop through, create and position cubes
for ( var y:int = 0; y < myScale.y; y ++ )
{
for ( var x:int = 0; x < myScale.x; x ++ )
{
var brick : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube); // instantiate cube
brick.renderer.material.color = Color.red;
brick.AddComponent( Rigidbody );
brick.rigidbody.mass = 1.0;
brick.AddComponent( DestroyInTime ); // attach self-destruct script
brick.active = false; // de-activate object
rubble.Add( brick ); // add cube to rubble list
// set position
var brickPos : Vector3 = Vector3( x + 0.5 - (myScale.x * 0.5), y + 0.5 - (myScale.y * 0.5), 0.0 ); // myTransform.position +
var worldBrickPos : Vector3 = Vector3( myTransform.position.x + brickPos.x,
myTransform.position.y + brickPos.y,
myTransform.position.z + brickPos.z );
brick.transform.position = worldBrickPos; // brickPos
}
}
}
function OnTriggerEnter( other : Collider )
{
TurnToRubble();
}
function TurnToRubble()
{
for ( var i: int = 0; i < rubble.Count; i ++ )
{
rubble*.active = true;*
-
}*
-
// disable this main wall, it’s destroyed!*
-
this.gameObject.active = false;*
}
You most likely have an error! This is ok, I shall explain and fix it.
I have set this wall up to destroy all the bricks after a set time period. After a while you want to fade away your debris. For now this simply destroys them after a set time period (plus some randomness). It is important that you call this script DestroyInTime (This will remove the error!) :
#pragma strict
var theDelay : float = 30.0;
function Start()
{
- Invoke( “TimeToGo”, theDelay + Random.Range( 0.1, 5.0 ) );*
}
function TimeToGo()
{
- Destroy( gameObject );*
}
Finally to shoot some cannonballs you can use this, simply attach it to the camera :
#pragma strict
var theCamera : Transform;
var force : float = 50.0;
function Start()
{
- theCamera = Camera.main.transform;*
}
function Update()
{
function Cannonball()
{
- var go : GameObject = GameObject.CreatePrimitive(PrimitiveType.Sphere);*
- go.renderer.material.color = Color.yellow;*
_ go.transform.position = theCamera.position + (theCamera.forward * 2);_
- go.AddComponent( Rigidbody );*
- go.rigidbody.mass = 5.0;*
- go.AddComponent( DestroyInTime ); // attach self-destruct script*
_ go.rigidbody.velocity = theCamera.forward * force;_
}
function RotateCam()
{
-
var rotX : float = Input.GetAxis( “Horizontal” );*
-
var rotY : float = Input.GetAxis( “Vertical” );*
-
theCamera.rotation.eulerAngles.y = theCamera.rotation.eulerAngles.y + rotX;*
-
theCamera.rotation.eulerAngles.x = theCamera.rotation.eulerAngles.x + rotY;*
}
Now hit Play =]
as you hit the wall, The first wall is disabled, and all the ‘bricks’ that make up the wall are enabled. These bricks have physical properties (rigidbodies), and as explained before, it is often resourceful to ‘clean up’ the scene so the GPU/CPU doesn’t have to work harder and harder for more bricks, so they will start to disappear between 30 and 35 seconds.
Now you can simply duplicate the wall, and start building a castle =]
This is just a visual demonstration to explain a concept of swapping out one object with others, this script really should not be used in a real project.
The concept should now be clear. Make your castle out of sections , these sections need 2 things. 1/ a 3d model 2/ several pieces that form the 3dmodel, but are individual physical pieces. When the section needs to be affected by gameplay, simply swap out the 3d model and swap in the pieces.
This link may help you do that.
But it’s not easy at all 