How can i kill my enemy?,How to kill enemy in unity 3d.
In my game, i can shoot bullet but i cannot kill my enemy. I want enemy disappear after i shoot him. Please help me. **I'm using two codes. One is Throwobject and another is monsterattack.
Throwobject
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<AudioSource>();
}
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<Rigidbody>().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<MonsterAttack>().health -= 10;
//hit.collider.gameObject.GetComponent<Animation>().Play("HumanoidJumpUp");
}
}
}
}
}
and here is monsterattack
using UnityEngine;
using System.Collections;
public class MonsterAttack : MonoBehaviour {
private GlobalInformation gi3;
public int health;
public int hit;
public Transform target;
//public int maxDistance;
public int targetDistance;
private float enemy2char;
private Transform myTransform;
// 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) {
gi3.addscore (20);
Destroy (gameObject);
}
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 want to make bullet decrease health -20 when it hits monster. Thank you again. ,
Answer by exzizt · Mar 21, 2018 at 02:15 AM
You can acheive this effect mutliple ways. One way is to do this:
Have a bool for your enemy object called "isDead". When you reduce from your enemy health, check to see if health is
Also, to make your enemy "disappear", if their health is
Thank you for answer me. Can you tell me how code should be? Thank you
And... If i do that way, can i put code in throwobject code?
Ins$$anonymous$$d of doing this:
hit.transform.GetComponent().health -= 10;
You could do:
hit.transform.GetComponent().AddHealth(-10);
And then make the method AddHealth in monster attack, like so:
public void AddHealth(int amount)
{
health += amount;
if (health <= 0)
{
// Do the stuff here you want to do when your enemy is "dead".
isDead = true;
// $$anonymous$$ake the enemy "disappear" by destroying the enemy gameobject.
Destroy(gameObject);
}
}
Hi I just put
public bool isDead; and your stuff above but the enemy still doesn't disappear. What should i do??' Thank you