- Home /
Bullet not destroying on collision
for some reaon my bullets are not doing any damage, and i know they are hitting becaause i put a rigid body on my target and i can tell it gets hit here is my bullet code
function Start() { Destroy(gameObject, 5); } var dmg:float = 20;
function OnTriggerEnter(hit:Collider){
if(hit.tag == "Enemy"){
hit.transform.SendMessage("Damage",dmg);
}
}
here is my ai code
var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake() { myTransform = transform; //cache transform data for easy access/preformance } function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
var health:float = 1;
function Damage(dmg:float){ health -= dmg; } if(health <=0){ Destroy (gameObject); }
Answer by zkerr577 · Nov 24, 2019 at 01:06 AM
Add this to whatever code is attached to your bullet.
This tells whatever object it's attached to to destroy itself on contact with anything.
private void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
FPS Tutorial from unity ??? 0 Answers
Health and damage reciever 1 Answer
Question about fliping a character 1 Answer
Enemy deals damage to player on contact 2 Answers