Player takes damage through collider tag
Hello All ! im trying to get this script to work and to no avail. basically what i am trying to do is to make my player loose health when a collider (attached to a bullet) hits the player. ill put below both my Enemy and Player scripts below along with the collider bullet. please this is something ive been working on for awhile and would like it to work properly
Player :
using UnityEngine;
using System.Collections;
public class Player : Creature {
bool block;
public static Player player;
GameObject prefab;
public static Transform opponent;
public Transform Front;
public GameObject target;
public GameObject Engine;
public AudioClip[] audioClip;
public GameObject Death;
public GameObject turret;
public GameObject AutoTurret;
public GameObject AutoTurretPassive;
void PlaySound(int clip){
GetComponent<AudioSource>().clip = audioClip [clip];
GetComponent<AudioSource>().Play();
}
void Awake(){
health = maxHealth;
player = this;
}
// Use this for initialization
void Start () {
Engine.SetActive (false);
prefab = Resources.Load ("shot") as GameObject;
Death.SetActive (false);
target.SetActive (false);
AutoTurret.SetActive (false);
}
// Update is called once per frame
void Update () {
//Attack ();
if (AutoTurretPassive.activeSelf) {
AutoTurret.SetActive (true);
} else {
AutoTurret.SetActive (false);
}
if (Input.GetMouseButton (0)) {
Engine.SetActive (true);
} else {
Engine.SetActive (false);
}
if (player.health <= 0) {
Death.transform.position = gameObject.transform.position;
Death.SetActive (true);
Destroy (this.gameObject);
}
if (opponent != null && Vector3.Distance (opponent.position, transform.position) < range) {
turret.transform.LookAt (opponent);
AutoTurret.transform.LookAt (opponent);
target.SetActive (true);
target.transform.position = opponent.transform.position;
} else {
target.SetActive (false);
turret.transform.LookAt (Front);
AutoTurret.transform.LookAt (Front);
}
}
void laserShot () {
GameObject shot = Instantiate (prefab)as GameObject;
shot.transform.position= transform.position+transform.forward*4;
Rigidbody rb=shot.GetComponent<Rigidbody>();
rb.velocity = transform.forward * 100;
}
void OnMouseOver (){
}
protected override void Attack(){
if (!block){
if (opponent != null && Vector3.Distance (opponent.position, transform.position) < range) {
if (Input.GetMouseButton (1)) {
Debug.Log ("Auto HIT");
//opponent.GetComponent<Enemy> ().GetHit (damage);
block = true;
Invoke ("UnBlock", attackSpeed);
}
}
}
//Auto Turret Attack
if (AutoTurret.activeSelf) {
if (!block) {
if (opponent != null && Vector3.Distance (opponent.position, transform.position) < range) {
Debug.Log ("Auto HIT");
//opponent.GetComponent<Enemy> ().GetHit (damage);
block = true;
Invoke ("UnBlock", CoolDown);
}
}
}
}
void UnBlock(){
block = false;
}
public void GetHit(){
}
}
Enemy:
using UnityEngine;
using System.Collections;
public class Enemy : Creature {
GameObject prefab;
public int scoreValue = 1;
public static Enemy enemy;
public float chasingRange;
NavMeshAgent navAgent;
Player player;
bool block;
public GameObject death;
new AudioSource audio;
public AudioClip laser;
public AudioClip defeated;
void Awake(){
enemy = this;
health = maxHealth;
}
// Use this for initialization
void Start () {
audio = GetComponent<AudioSource>();
prefab = Resources.Load ("shotTwo") as GameObject;
navAgent = GetComponent<NavMeshAgent> ();
player = Player.player;
death.SetActive (false);
}
void FollowPlayer(){
if (IsInRange (chasingRange)) {
navAgent.SetDestination (player.transform.position);
} else
navAgent.SetDestination (transform.position);
}
void laserShot () {
GameObject shotTwo = Instantiate (prefab)as GameObject;
shotTwo.transform.position= transform.position+transform.forward*4;
Rigidbody rb=shotTwo.GetComponent<Rigidbody>();
rb.velocity=transform.forward*150;
}
void Death(){
if (health <= 0) {
death.transform.position = gameObject.transform.position;
death.SetActive (true);
AudioSource.PlayClipAtPoint (defeated, new Vector3(0,0,0), volume:.55f);
Destroy (gameObject);
}
}
// Update is called once per frame
void Update () {
FollowPlayer ();
Attack ();
Death ();
if (health <= 0) {
ScoreManager.score += scoreValue;
}
if (player.health <= 0) {
ScoreManager.score = 0;
Destroy (this.gameObject);
}
if (Vector3.Distance (player.transform.position, transform.position) < range) {
transform.LookAt (player.transform);
}
}
bool IsInRange(float range){
if (Vector3.Distance (player.transform.position, transform.position) < range) {
return true;
} else
return false;
}
void OnMouseOver(){
Debug.Log ("Selected" + gameObject.name);
Player.opponent = transform;
}
protected override void Attack(){
if (IsInRange(range)) {
if(!block){
laserShot ();
//player.GetHit (damage);
audio.PlayOneShot (laser);
block = true;
Invoke ("UnBlock", attackSpeed);
//Debug.Log ("ATTACK");
}
}
}
void UnBlock(){
block = false;
}
}
Bullet :
using UnityEngine; using System.Collections;
public class ShotVanish : MonoBehaviour { Player player;
void Start(){
player = Player.player;
}
void OnTriggerEnter(Collider col){ if (Player.opponent != null){ if (col.tag == "Solid") { Player.opponent.GetComponent ().GetHit (player.damage); Destroy(this.gameObject); } }
if (col.tag == "Enemy") {
if (Player.opponent != null){
Player.opponent.GetComponent<Enemy> ().GetHit (player.damage);
Destroy(this.gameObject);
}
}
if (col.tag == "Boundry") {
Destroy (this.gameObject);
}
if (col.tag == "Player") {
Debug.Log ("HIT");
player.GetHit (GetComponent<Enemy> ().damage);
Destroy(this.gameObject);
}
}
}
Comment