- Home /
weapon collision with characters
I have been trying and trying to get my character to attack and get damage dealt to him any ideas on what i'm doing wrong. here are my scripts.
attached to the enemy weapon var Player : GameObject;
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.name == "Player"){
gameObject.Find("Player").SendMessage("decreasehealth1");
}
}
attached to the player
var playerhealth = 100;
var myskin : GUISkin;
function decreasehealth1(){
playerhealth -= 5;
}
function decreasehealth2(){
playerhealth -= 7;
}
function decreasehealth3(){
playerhealth -= 10;
}
function OnGUI(){
GUI.skin = myskin;
if (playerhealth >= 100){
GUILayout.Box("", "fullhealth");
}
if (playerhealth >= 90){
GUILayout.Box("", "90%health");
}
if (playerhealth >= 80){
GUILayout.Box("", "80%health");
}
if (playerhealth >= 70){
GUILayout.Box("", "70%health");
}
if (playerhealth >= 60){
GUILayout.Box("", "60%health");
}
if (playerhealth >= 50){
GUILayout.Box("", "50%health");
}
if (playerhealth >= 40){
GUILayout.Box("", "40%health");
}
if (playerhealth >= 30){
GUILayout.Box("", "30%health");
}
if (playerhealth >= 20){
GUILayout.Box("", "20%health");
}
if (playerhealth >= 10){
GUILayout.Box("", "10%health");
}
if (playerhealth >= 0){
GUILayout.Box("", "0%health");
}
}
any help would be apreciated :}
Comment
Best Answer
Answer by fafase · Apr 25, 2012 at 06:40 PM
If I were you, since health is one and only for your player, you should use a static var. that makes easier and SendMessage is quite expensive function
function OnControllerColliderHit(hit : ControllerColliderHit) {
if(hit.gameObject.name == "Player"){
PlayerScriptName.health-=5;
}
}
Now a word on the collision function you are using, it will work only if you are moving. Maybe that is why it does not do anything. use OnCollisionEnter(other:Collision) instead.
Instead try on the player
function OnCollisionEnter(other:Collision){
if(other.gameObject.name=="Sword")
health-=5;
else if (other.gameObject.name == "Bullet")
health -= 20;
}