- Home /
Question by
MiraiTunga · Feb 22, 2013 at 10:31 AM ·
efficiencyshort
Trying to achevie Shorter code less repetition
this is the code iam using but cant figure out a way to make it short
public var health = 200;
function OnTriggerEnter(other:Collider){
var ammorPls: GameObject[] = GameObject.FindGameObjectsWithTag("ammor");
// Assign the builtin array to a js Array
var ammorPlsB = new Array (ammorPls);
if(other.tag=="bullet"){
health = health-10;
}
if(health<10) {
//remove ammor sections from range
for (var a = 0; a < 3; a++) {
ammorPlsB[a].rigidbody.isKinematic=false;
ammorPlsB[a].rigidbody.useGravity=true;
ammorPlsB[a].transform.parent = null;
ammorPlsB[a].collider.enabled=true;
}
}
if(health<20) {
//remove ammor sections from range
for (var b = 3; a < 6; a++) {
ammorPlsB[b].rigidbody.isKinematic=false;
ammorPlsB[b].rigidbody.useGravity=true;
ammorPlsB[b].transform.parent = null;
ammorPlsB[b].collider.enabled=true;
}
}
......
Comment
Answer by Bunny83 · Feb 22, 2013 at 10:48 AM
I would do something like:
static function DropObject(obj : GameObject)
{
obj.rigidbody.isKinematic = false;
obj.rigidbody.useGravity = true;
obj.transform.parent = null;
obj.collider.enabled = true;
}
function OnTriggerEnter(other:Collider)
{
var ammorPls: GameObject[] = GameObject.FindGameObjectsWithTag("ammor");
var ammorPlsB = new Array (ammorPls);
if(other.tag=="bullet")
{
health = health-10;
var index : int = health / 10;
index = Mathf.Clamp(index*3, 0, ammorPlsB.length-2);
for (var a = 0; a < 3; a++) {
DropObject(ammorPlsB[index + a])
}
}
}
thanks for the answer ..i can kinda see how it can wrk tried implementing it tho and had a few problems i think iam just gona do it the long way
Your answer
Follow this Question
Related Questions
Script Efficiency 1 Answer
How to make code more efficient 3 Answers
How does code licensing for Unity works? What happens when I post for evaluation / showcase? 1 Answer
Instantiate as child 3 Answers
Scripting a Konami Code 1 Answer