- Home /
Randomize health loss
Hello, i have a C# code, all i wanna know is how to randomize the health loss of the player here is the code
using UnityEngine; using System.Collections; public class Enemyattack : MonoBehaviour { 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(attackTimer ==0){ Attack(); attackTimer = coolDown; } } private 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); if(distance < 4){ if(direction > 0){ Health eh = (Health)target.GetComponent("Health"); eh.AddjustCurrentHealth(-10); } } } }
If anybody could help that would be great,
Thanks
Answer by FLASHDENMARK · Mar 28, 2012 at 07:52 PM
You could use Random.Range.
An example:
var minRandomHealth : float = 2.5;
var maxRandomHealth : float = 2.5;
var health : float = 100;
function Awake (){
health -= Random.Range(-minRandomHealth, maxRandomHealth);
}
This is JS, but it is more or less the same in C#.
Your answer
Follow this Question
Related Questions
Player attack script + enemy health 1 Answer
how do I edit enemy stats 3 Answers
How to stop enemies dying on spawn 1 Answer
How to make my player not lose health when attacking the enemy? 2 Answers
Problem with HealthBar 1 Answer