Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Zandlie · Dec 21, 2015 at 04:36 PM · collisionenemytagrpghealth

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnCollisionEnter2D not working, Again! 0 Answers

Enemy Can't Take Damage!!! 0 Answers

Looking at closest visible object with tag 0 Answers

How can i kill my enemy?,How to kill enemy in unity 3d. 1 Answer

[Solved] I can't call Method from Referenced Script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges