after adding in the line ‘Destroy(gameObject);’ to the bullet script the enemy no longer shoots. Why is this? I added that line of code so the bullet cannot go through walls.
Enemy Bullet Script:
var Dammage = 50;
function OnCollisionEnter (info : Collision)
{
info.transform.SendMessage("ApplyDammage1", Dammage,
SendMessageOptions.DontRequireReceiver);
Destroy(gameObject, 1);
}
function Update ()
{
var translation : float = Time.deltaTime * 60;
transform.Translate(0,0,translation);
}
AI Script:
var Distance;
var Target : Transform;
var ShootRange = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
var projectile : Rigidbody;
var fireRate : float = 1;
var FireRange = 0-100;
private var nextFire : float = 0.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < ShootRange)
{
GetComponent.().material.color = Color.yellow;
lookAt();
}
How, and where, do you instantiate the bullet? It sounds like you instantiate it at a position where it collides with something immediately. Either check for that in the script (if info.gameObject.tag etc) or move the position somewhere where it can not collide at spawn.
You call Destroy twice now, and the first call delays the destroy by one second, I also assume the second one doesn’t do anything as Destroy should only be called ones. So that explains why code-wise, the bullet goes “through” the wall. The second reason is that the physics engine does not do accurate calculations on fast objects, so objects can clip through. It might be better to use a raycast as the bullet, unless you have a reason for using a physical object.
That said, also make sure the bullet has a rigidbody attached, otherwise the OnCollisionEnter method will not be called at all.