Bullet fired at enemy only applying damage when the enemy moves

Hi guys,

I have the following script on some bullets that automatically home in on the nearest enemy. Each bullet has a sphere trigger

Bullet.js:

#pragma strict

var damageValue : int = 2;

var lifetime : float = 15.0;

var speed : float = 10;

private var enemies : GameObject[];

private var target : GameObject;

private var elapsedTime : float = 0;

...

function FixedUpdate () {

	if (target)
	{
		transform.LookAt(target.transform);
		Debug.Log("Targeting an enemy");
	}
	else
	{
		Debug.Log("Target not found, proceeding straight from player");
	}
	
	transform.Translate(Vector3.forward * speed * Time.deltaTime);
	
	elapsedTime += Time.deltaTime;
    if (elapsedTime >= lifetime)
     	Destroy(gameObject);
}

function OnTriggerEnter(other : Collider)
{
	if (other.tag == "Enemy")
		other.SendMessage("ApplyDamage", damageValue);
	
	if (other.tag != "Player")
		Destroy(gameObject);
}

The bullets home in on enemies as predicted. Each enemy has the following code.

EnemyDamage.js:

var hitPoints = 3;

var explosionPrefab : Transform;
var deadModelPrefab : Transform;
var healthPrefab : DroppableMover;
var dropMin = 0;
var dropMax = 0;

var scoreValue : int = 100;

...

var isBoss : boolean = false;

private var dead = false;

function ApplyDamage (damage : int)
{
	Debug.Log("Enemy Taking Damage");
	// we've been hit, so play the 'struck' sound. This should be a metallic 'clang'.
	if (audio && struckSound)
		audio.PlayOneShot(struckSound);

	if (hitPoints <= 0)
		return;

	hitPoints -= damage;
	if (!dead && hitPoints <= 0)
	{
		Die();
		dead = true;
	}
}

function Die ()
{	
	...
}

...

If an enemy is not moving (e.g. the enemy is just standing there (e.g. in the idle state defined in the Lerpz 3rd Person Tutorial’s EnemyDamage.js script), the bullet just goes to the enemy’s position and doesn’t apply any damage until the enemy starts moving. I want the bullet to apply damage regardless of state.

Also, there is a boss with a custom flying script. That boss does not take any damage upon being shot. Here is the boss’s script in its entirety, showing that the boss is in constant motion.

FlyingBoss.js:

#pragma strict

var speed : float = 1.0;

var attackIntervalMin : float = 1.0;

var attackIntervalMax : float = 5.0;

var bullet : GameObject;

private var waypoints : GameObject[];

private var target : Transform;

private var elapsedTime : float = 0;
private var attackInterval : float = 0;

function SelectWaypoint()
{

	var numValues = waypoints.Length;
	
	var wayPointIndex = Random.Range(0, numValues - 1);
	
	if (waypoints[wayPointIndex].transform != transform)
		target = waypoints[wayPointIndex].transform;
	else
	{
		if (waypoints[wayPointIndex].transform == waypoints[numValues-1].transform)
			target = waypoints[wayPointIndex -1].transform;
		else
			target = waypoints[wayPointIndex + 1].transform;
	}
	
}

function Start () 
{

	waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
	
	SelectWaypoint();
	
	animation["fly"].wrapMode = WrapMode.Loop;
	animation.Play("fly");
	
	GetRandomInterval();

}

function GetRandomInterval()
{
	attackInterval = Random.Range(attackIntervalMin, attackIntervalMax);
}

function LaunchFireball()
{
	// Instantiate the projectile at the position and rotation of this transform
    var clone : Transform;
    clone = Instantiate(bullet.transform, transform.position, transform.rotation);
}

function FixedUpdate () 
{
	// Timer code for attacks
	
	elapsedTime += Time.deltaTime;
	
	if (elapsedTime >= attackInterval)
	{
		LaunchFireball();
		
		elapsedTime = 0;
		
		GetRandomInterval();
	}
	
	// Movement code
	
	if (target)
	{
		transform.LookAt(target); // TODO: replace this with smoother rotation code
		
		transform.Translate(Time.deltaTime * speed * Vector3.forward, Space.Self);
	}
	
	Debug.Log("Boss position x:" + transform.position.x + "y:" + transform.position.y + "z" + transform.position.z );
	
	// Once at target location, find a new one
	
	var targetDistance = Vector3.Distance(transform.position, target.position);
	
	Debug.Log("Distance to target:" + targetDistance);
	if (targetDistance < 0.25)
		SelectWaypoint();
}

All help will be appreciated.

MachCUBED

Try adding a kinematic rigid body to your enemy collider and see if that helps. Perhaps the collision message is not triggered when the enemy remains still.