- Home /
Need Help with rebuildable barricade system like in Call Of Duty Zombies
so this is what I need. I don't know much about programming stuff and need help with a script like this. Please don't get angry I just want ideas or suggestions about how can I make something like this. Tutorials or Examples accepted :D I really need some help. Thanks. Edit : Umm, I think i solved it myself but I am still having problems and i know what is my problem read the code please, i wrote something which will check barricade Health and teleport parts away. But as it is true it keeps teleporting parts away and i can't fix it. Edit2 : I solved that problem too, i still wonder if you can make a shorter version of this because this one lags a bit.
#pragma strict //my old code
@script RequireComponent( AudioSource )
public var barricadeHealth : int = 0;
public var thePlayer : Transform;
public var theEnemy : Transform;
public var barricade1a : Transform;
public var barricade2a : Transform;
public var barricade3a : Transform;
public var barricade4a : Transform;
public var barricade5a : Transform;
var isBuilt : boolean = true;
var canRepair : boolean = false;
var attacking : boolean = false;
var breaking : boolean = false;
var canBreak : boolean = false;
public var breakingSound : AudioClip;
public var repairingSound : AudioClip;
function Start()
{
if ( thePlayer == null )
{
thePlayer = GameObject.Find( "Player" ).transform;
}
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Zombie" ).transform;
}
}
function Update () {
if (barricadeHealth<0)
{
barricadeHealth=0;
isBuilt=false;
breaking=false;
}
if (barricadeHealth>0)
{
isBuilt=true;
}
if (breaking && canBreak)
{
Breaking();
}
if(isBuilt)
{
if(barricadeHealth>5)
{
barricadeHealth=5;
}
if(barricadeHealth==4)
{
barricade5a.transform.Translate(0,-10,0);
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricade5a.transform.Translate(0,10,0);
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricadeHealth+=1;
}
}
if(barricadeHealth==3)
{
barricade4a.transform.Translate(0,-10,0);
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade4a.transform.Translate(0,10,0);
}
}
if(barricadeHealth==2)
{
barricade3a.transform.Translate(0,-10,0);
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade3a.transform.Translate(0,10,0);
}
}
if(barricadeHealth==1)
{
barricade2a.transform.Translate(0,-10,0);
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade2a.transform.Translate(0,10,0);
}
}
}
if(!isBuilt)
//if barricadeHealth equals 0
barricade1a.transform.Translate(0,-10,0);
if(Input.GetKeyUp(KeyCode.E) && !isBuilt && canRepair)
{
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade1a.transform.Translate(0,10,0);
barricadeHealth+=1;
}
}
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.name == "Player")
{
canRepair = true;
if(other.gameObject.name == "Zombie")
{
breaking = true;
}
else
{
breaking = false;
attacking = true;
}
}
}
function OnTriggerExit (other : Collider)
{
if(other.gameObject.name == "Player")
{
canRepair = false;
if(other.gameObject.name == "Zombie")
{
breaking = false;
}
else
{
breaking = true;
attacking = false;
}
}
}
function Breaking()
{
canBreak=false;
yield WaitForSeconds (5);
barricadeHealth-=1;
audio.PlayClipAtPoint( breakingSound, theEnemy.position );
canBreak=true;
if (barricadeHealth>0)
{
isBuilt=true;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma strict
//new working code
@script RequireComponent( AudioSource )
public var barricadeHealth : int = 0;
public var thePlayer : Transform;
public var theEnemy : Transform;
public var barricade1a : Transform;
public var barricade2a : Transform;
public var barricade3a : Transform;
public var barricade4a : Transform;
public var barricade5a : Transform;
var isBuilt : boolean = true;
var canRepair : boolean = false;
var attacking : boolean = false;
var breaking : boolean = false;
var canBreak : boolean = false;
public var breakingSound : AudioClip;
public var repairingSound : AudioClip;
var barricadeDown1a : boolean = false;
var barricadeDown2a : boolean = false;
var barricadeDown3a : boolean = false;
var barricadeDown4a : boolean = false;
var barricadeDown5a : boolean = false;
function Start()
{
isBuilt = true;
barricadeDown1a = false;
if ( thePlayer == null )
{
thePlayer = GameObject.Find( "Player" ).transform;
}
if ( theEnemy == null )
{
theEnemy = GameObject.Find( "Zombie" ).transform;
}
}
function Update () {
if (barricadeHealth<0 || barricadeHealth==0)
{
barricadeHealth=0;
isBuilt=false;
breaking=false;
//note that there is no barricadeDown1a = true; because it causes a bug in script
barricadeDown2a = true;
barricadeDown3a = true;
barricadeDown4a = true;
barricadeDown5a = true;
}
if (barricadeHealth>0)
{
isBuilt=true;
}
if (breaking && canBreak)
{
Breaking();
}
if(isBuilt)
{
if(barricadeHealth>5 || barricadeHealth==5)
{
barricadeHealth=5;
barricadeDown1a = false;
barricadeDown2a = false;
barricadeDown3a = false;
barricadeDown4a = false;
barricadeDown5a = false;
}
if(barricadeHealth==4)
{
barricadeDown1a = false;
barricadeDown2a = false;
barricadeDown3a = false;
barricadeDown4a = false;
if (!barricadeDown5a)
{
barricade5a.transform.Translate(0,-10,0);
barricadeDown5a = true;
}
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricade5a.transform.Translate(0,10,0);
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricadeHealth+=1;
}
}
if(barricadeHealth==3)
{
barricadeDown1a = false;
barricadeDown2a = false;
barricadeDown3a = false;
barricadeDown5a = true;
if (!barricadeDown4a)
{
barricade4a.transform.Translate(0,-10,0);
barricadeDown4a = true;
}
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade4a.transform.Translate(0,10,0);
}
}
if(barricadeHealth==2)
{
barricadeDown1a = false;
barricadeDown2a = false;
barricadeDown4a = true;
barricadeDown5a = true;
if (!barricadeDown3a)
{
barricade3a.transform.Translate(0,-10,0);
barricadeDown3a = true;
}
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade3a.transform.Translate(0,10,0);
}
}
if(barricadeHealth==1)
{
barricadeDown1a = false;
barricadeDown3a = true;
barricadeDown4a = true;
barricadeDown5a = true;
if (!barricadeDown2a)
{
barricade2a.transform.Translate(0,-10,0);
barricadeDown2a = true;
}
if(Input.GetKeyUp(KeyCode.E) && isBuilt && canRepair)
{
barricadeHealth+=1;
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade2a.transform.Translate(0,10,0);
}
}
}
if(!isBuilt)
//if barricadeHealth equals 0
if (!barricadeDown1a)
{
barricade1a.transform.Translate(0,-10,0);
barricadeDown1a = true;
}
if(Input.GetKeyUp(KeyCode.E) && !isBuilt && canRepair)
{
isBuilt = true;
audio.PlayClipAtPoint( repairingSound, thePlayer.position );
barricade1a.transform.Translate(0,10,0);
barricadeHealth+=1;
}
}
function OnTriggerEnter (other : Collider)
{
if(other.gameObject.name == "Player")
{
canRepair = true;
if(other.gameObject.name == "Zombie")
{
breaking = true;
}
else
{
breaking = false;
attacking = true;
}
}
}
function OnTriggerExit (other : Collider)
{
if(other.gameObject.name == "Player")
{
canRepair = false;
if(other.gameObject.name == "Zombie")
{
breaking = false;
}
else
{
breaking = true;
attacking = false;
}
}
}
function Breaking()
{
canBreak=false;
yield WaitForSeconds (5);
barricadeHealth-=1;
audio.PlayClipAtPoint( breakingSound, theEnemy.position );
canBreak=true;
}
Answer by Slendyberkay · Dec 08, 2013 at 04:52 PM
Any shorter version (Ä° learned that i can use gameObject enable/disable)