- Home /
kill enemy with Prefab bullet
Okay i want to kill an enemy using prefab bullets not raycast i have the script where the enemy walks towards me but i am stuck on how to make the prefab bullets do any damage to the enemy below is the script
var lookAtTarget : Transform; var speed : int; var range : int; var damp : float; var ENEMY_HEALTH = 30; var nextFire = 1; private var fireRate = 0.0; var enemyBullet : Transform; var death : Transform ; var health : Transform; function Awake() { if (!lookAtTarget) { lookAtTarget = GameObject.FindWithTag("Player").transform; } } function Update() { if(ENEMY_HEALTH <= 0) { Instantiate(death, transform.position, transform.rotation); Instantiate(health, transform.position, transform.rotation); Debug.Log("DEAD"); Destroy(gameObject); } if(lookAtTarget && CanAttackTarget()) { //transform.LookAt(lookAtTarget); var rotate = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotate, damp); transform.Translate (0,0,speed*Time.deltaTime); if(Time.time >= nextFire) { nextFire = Time.time + fireRate; var bullet = Instantiate(enemyBullet, transform.position, transform.rotation); bullet.rigidbody.velocity = transform.forward * speed; } } } function CanAttackTarget() { //check if in range if(Vector3.Distance(transform.position, lookAtTarget.position) > range) { return false; } return true; } function OnParticleCollision () { Debug.Log("hit"); ENEMY_HEALTH--; }
Answer by Hybris · Jan 18, 2012 at 05:39 PM
If you want to use prefab bullets, why do you use particlecollision, you could do this:
function OnCollisionEnter(bullet : Collision){
if(bullet.gameObject.tag == "Insert bullet tag here"){
ENEMY_HEALTH--;
}
}
This should work.
Ok and where are you activating CanAttackTarget?
You are saying if(LookAtTarget && CanAttacktarget())
With that, 2 things : CanAttackTarget in an if should never have this (), and you arent activating CanAttackTarget anywhere, so its never true.
This is not my script :/ i don't know how to code $$anonymous$$uch so i dunno how to do what you just said :L sorry if i seem dumb but im learning slowly hehe any chance to could put the code you just wrote in to the code i have ??
yeah, but you should really make your own script if your just learning the ropes, if you want i could gice you a easy to understand health script. Replace OnParticleCollision function with the OnCollisionEnter code i just gave you, try and see if it works
Your answer
![](https://koobas.hobune.stream/wayback/20220613063133im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera Pan 1 Answer
how do i use two animations on the same object?? 1 Answer
VUFORIA: Playing an animation for a instanced model? 2 Answers