- Home /
Player Character Health
hey guys. i have a player character script from BurgZergArcade with weapon meshes along with a player combat script. i want each mesh to take a different amount of health away from the enemy using this code:
EnemyHealth eh = (EnemyHealth)_enemy.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(-45);
also, i want to send a message to the player combat script from the functions of the player character script. do you have any suggestions? thanks
Answer by Loius · Aug 01, 2012 at 06:59 PM
I'd suggest using an actual tutorial instead of grabbing code and worrying your way through it. The changes you're looking for are very simple.
(change -45 to a variable, call a function)
Thanks. how do i make this work with the weapon meshes and this script?
using UnityEngine; using System.Collections;
public class PlayerCombat : $$anonymous$$onoBehaviour { public GameObject target; public float attackTimer; public float coolDown;
// Use this for initialization void Start () { attackTimer = 0; coolDown = 2.0f;
}
// Update is called once per frame void Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;
if(attackTimer < 0) attackTimer = 0;
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F)) { if(attackTimer == 0) { Attack(); attackTimer = coolDown; } } }
public void Attack() { float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
Debug.Log(direction);
if(distance < 2f) { if(direction > 0) { Send$$anonymous$$essage("$$anonymous$$axDamage");
if(distance < 2f) { if(direction > 0) { Send$$anonymous$$essage("Damage");
if(distance < 2f) { if(direction > 0) { Send$$anonymous$$essage("$$anonymous$$inDamage");
EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
if(eh.curHealth == 0) { Send$$anonymous$$essage("replaceWithRagdoll"); } }
} } }
} } } }
Answer by IndieScapeGames · Aug 01, 2012 at 07:36 PM
To expand on Vicenti's answer, you want to assign -45 to a variable, and pass that in the method call, and not specifically just a number.
int damageInflicted = -45;
EnemyHealth eh = (EnemyHealth)_enemy.GetComponent("EnemyHealth");
eh.AddjustCurrentHealth(damageInflicted);
/*
*Check spelling of AddjustCurrentHealth with the spelling of the method
*The correct spelling of addjust is adjust
*/
Your answer
Follow this Question
Related Questions
Taking a hit 3 Answers
Enemy not taking damage on collisions. 2 Answers
Lose health on collision 1 Answer
Player Health 3 Answers
Adding a counter? 1 Answer