Question by
Mcboss762 · Jun 27, 2018 at 03:44 AM ·
c#scripting problemdamagegameover
I need help with my Damage + GameOver Script
Hi so I'm making a game where enemies follow you around and I would like to make it so when they get within a certain range of you, they decrease your health by 1 and you have 3 health. This is what my script looks like so far.
using UnityEngine;
using System.Collections;
public class ZombieFollow : MonoBehaviour {
public GameObject ThePlayer;
public float TargetDistance;
public float AllowedRange = 300;
public float TriggerDistance = 5;
public GameObject TheEnemy;
public float EnemySpeed;
public int AttackTrigger;
public RaycastHit Shot;
public int IsAttacking;
public GameObject ScreenFlash;
public AudioSource Hurt01;
public int PainSound;
public GameObject Collider;
Vector3 myPosition;
Vector3 enemyPosition;
void Update() {
transform.LookAt (ThePlayer.transform);
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), out Shot)) {
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange) {
EnemySpeed = 0.09f;
if (AttackTrigger == 0) {
transform.position = Vector3.MoveTowards (transform.position, ThePlayer.transform.position, EnemySpeed);
}
//if (TriggerDistance >= TargetDistance) {
// ScreenFlash.SetActive (true);
//}
} else {
EnemySpeed = 0;
}
}
if (AttackTrigger == 1) {
EnemySpeed = 0;
ScreenFlash.SetActive (true);
}
//if(Vector3.Distance(myPosition, enemyPosition) < raneg)
//{
// ScreenFlash.SetActive (true);
//}
}
//void Update() {}
void OnTriggerEnter() {
AttackTrigger = 1;
}
void OnTriggerExit() {
AttackTrigger = 0;
}
IEnumerator EnemyDamage() {
IsAttacking = 1;
PainSound = Random.Range (1, 2);
yield return new WaitForSeconds (0.1f);
ScreenFlash.SetActive (true);
GlobalHealth.PlayerHealth -= 2;
if (PainSound == 1) {
Hurt01.Play ();
}
yield return new WaitForSeconds (0.05f);
ScreenFlash.SetActive (false);
yield return new WaitForSeconds (0.1f);
IsAttacking = 0;
}
}
Here is an image of what the game looks like: https://ibb.co/eTORS8 Thanks so much for any help is appreciated. Have a great day :)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612165549im_/https://answers.unity.com/themes/thub/images/avi.jpg)