- Home /
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
Answer by sirival · Dec 03, 2012 at 10:34 PM
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.
The normal enemies have kinematic rigidbodies already, so adding one to the enemies won't solve their problem. Adding a kinematic rigidbody to the flying boss didn't fix the problem either, even though the flying boss is always in motion. Adding a kinematic rigidbody to my player's bullets merely makes them disappear after being fired, so I removed them from the bullets.
Take a look at the table on the bottom of this link. $$anonymous$$aybe it will help you solve your problem and it is also the motivation of my response. http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html
I added the player's bullets to their own layer, added a rigidbody component to the bullets, and adjusted the collision matrix so that the player's bullets only collide with the enemies layer (e.g. the bullets layer only collides with the enemies layer). This fixed the problem for my normal enemies, who can be shot and killed at any time. However, the flying boss was missing its collider component, so adding a capsule collider solved the problem.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
I have a movement script, but how can i make it so i can move half the speed while in the air. 0 Answers
how to interpolate between 3 position in a infinite runner game (scripts) 0 Answers