- Home /
[Javascript] My script won't work..
I am trying to create a really simple AI for my zombie. It has no errors in console, however the bullets don't affect the health/zombies.
var LookAtTarget : Transform;
var damp : float = 6.0;
var zombieHealth : int = 100;
var minZombieHealth : int = 0;
var maxZombieHealth : int = 100;
var pistolDamage : int = 28;
function Update () {
ILookAtTarget();
Health();
}
function Health(){
if(zombieHealth < minZombieHealth){
Destroy(this.gameObject);
}
}
function OnCollisionEnter(collision:Collision){
if(collision.gameObject.tag.Equals("pistolBullet")){
zombieHealth -= pistolDamage;
}
}
function ILookAtTarget(){
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
transform.position = Vector3.MoveTowards(transform.position, LookAtTarget.transform.position, .08);
}
}
It probably has nothing to do with your script rather with your physics , sometimes when the bullet is too fast it goes through the object without triggering the collision event, try maybe a slow bullet and manually collide it your self. see if that triggers the collision.
I seem to have fixed it, the string for the tag was wrong, it was capitalized for some reason in tags.
How do I remove a question?
I'm not sure, but congratulations! every fixed bug is a step closer to completion!
Answer by tw1st3d · Aug 15, 2013 at 10:15 PM
if(collision.gameObject.tag.Equals("pistolBullet")){
Try using
if(collision.gameObject.tag.EqualsIgnoreCase("pistolBullet")){
This ensures that if the tag somehow is in all lowercase/uppercase it will identify it either way.
actually I don't know if UnityScript has EIC, so if EqualsIgnoreCase doesn't work, use
if(collision.gameObject.toLowerCase(tag).Equals("pistolBullet")){
EqualsIgnoreCase is not related to any language, it's a .NET function.
Java uses .equalsIgnoreCase(), but I'm not surprised that no other language uses it.