Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

How to make disappear enemy in unity 3d?

$
0
0
using UnityEngine; using System.Collections; public class ThrowObject : MonoBehaviour { public GameObject projectile; public AudioClip shootSound; public GameObject gun; public float throwSpeed = 1500f; private AudioSource source; private float volLowRange = .5f; private float volHighRange = 1.0f; void Awake () { source = GetComponent(); } void Update () { if (Input.GetButtonDown("Fire1")) { float vol = Random.Range (volLowRange, volHighRange); source.PlayOneShot(shootSound,vol); GameObject throwThis = Instantiate (projectile, transform.position, transform.rotation) as GameObject; gun.transform.DetachChildren (); throwThis.GetComponent().AddRelativeForce (new Vector3(0,0,throwSpeed)); } RaycastHit hit; Ray shooterRay = new Ray(transform.position, transform.forward); if(Input.GetButton("Fire2")){ Debug.DrawRay (transform.position, transform.forward, Color.green); if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){ if(hit.collider.tag == "Enemy"){ Debug.Log("Hit"); hit.transform.GetComponent().AddHealth(-10); //hit.collider.gameObject.GetComponent().Play("HumanoidJumpUp"); } } } } > using UnityEngine; using> System.Collections;>> public class MonsterAttack :> MonoBehaviour { private> GlobalInformation gi3; public int> health;> public bool isDead; public Transform target; //public int> maxDistance; public int> targetDistance; private float> enemy2char; private Transform> myTransform;> public void AddHealth(int amount)> {> health += amount; } // Use this for initialization void Start () { gi3 = GlobalInformation.gi; health = 100; myTransform = transform; GameObject go = GameObject.FindGameObjectWithTag("Player"); target = go.transform; } // Update is called once per frame void Update () { if (health <= 0) { //Do the stuff here you want to do when your enemy is "dead". isDead = true; //Make the enemy "disappear"by destroying the enemy gameobject. Destroy(gameObject); gameObject.SetActive(false); } Debug.DrawLine(target.position, myTransform.position, Color.yellow); enemy2char = Vector3.Distance (target.position, myTransform.position); /*if((enemy2char < maxDistance) && (enemy2char > targetDistance)) { //Move towards target Mover(); }*/ if (enemy2char <= targetDistance) { gi3.adjusthealth (-1); } } void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Player") { //other.GetComponent("PlayerHealth").AddjustCurrentHealth (); gi3.adjusthealth(-10); } if (other.gameObject.tag == "Enemy") { //other.GetComponent("PlayerHealth").AddjustCurrentHealth (); gi3.adjusthealth(-10); } } } I used those codes for attack enemy and make enemy disappear. But enemy doesn't disappear.. What i have to change for it? Thank you for reading

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>