- Home /
cube damage
im almost done with the gun and its script but the problem is the bullet.. it is bulletscript actually.. for the mean time my bullet is a cube, i need a psuedo code, idea or an actual code for the cube :)
when the cube touches an object(enemy), the enemy's hp will be decreased
Answer by AlucardJay · Mar 08, 2013 at 05:36 PM
A couple of options, the easiest being SendMessage :
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html
the other way would be to get a reference to the other script (GetComponent), but I think this is all you want.
No worries, just going through some old answers and checking up on them. Send$$anonymous$$essage is very handy for situations like this, though at some point you may have to learn GameObject.Find and GetComponent. These are the commands you'll need to master in order to find and access other scripts later on.
okie dokie gonna eat first :) ill check that when i come back
by the way, my cube script is working now.. that cube is my bullet :)
my problem now is the parenting, like in other games that you will pick up weapons.. i have the script but my script is good for only 2 child.. now how about when it is 2 or more ?
function Update () {
// Did the user press fire?
if (Input.Get$$anonymous$$eyDown("1")) {
SelectWeapon(0);
}
else if (Input.Get$$anonymous$$eyDown("2")) {
SelectWeapon(1);
}
}
function SelectWeapon (index : int) {
for (var i=0;i<transform.childCount;i++) {
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
Answer by Nelis · Mar 08, 2013 at 05:27 PM
You need to add a rigidbody to the cube, than make a prefab out of it. You make a bulletscript an add it to your weapon. When you press a button, you initiate the prefab and add a velocity to it. When your bullet touches the enemy, use the collider to check it and decrease the hp.