- Home /
Rockets bending around enemies?
Hello, all. I have a quick fps question. I have these rockets that the player can shoot, but I have the problem that they don't explode on my enemies, they bend around them and continue their path. I can tell they are going around the enemy because I've made a smoke trail and it shows them going around the enemy. the enemy has a sphere collider for a head(for head shots) and a character controller for the main body. The rocket has a simple box collider. Here is the rocket script.
@script AddComponentMenu("FPS / Weapons / Rockets")
@script RequireComponent(AudioSource);
var explosionRadius : float;
var damage : int;
var power : int;
var particleEffect : GameObject;
var sound : AudioClip;
var lifeTime : float;
var Speed : float;
var enemyRocket : boolean = false;
private var audioSource : AudioSource;
function Awake()
{
var Player : Transform = gameObject.FindWithTag("Player").transform;
if(enemyRocket)
transform.LookAt(Player);
audioSource = gameObject.GetComponent(AudioSource);
Destroy(gameObject, lifeTime);
}
function OnCollisionEnter(col : Collision)
{
if(col.gameObject.tag != "Enemy")
Explode();
}
function OnTriggerEnter(col : Collider)
{
if(col.gameObject.tag != "Enemy")
Explode();
}
function Update ()
{
rigidbody.velocity = transform.forward * Speed;
}
function Explode ()
{
var explosionPosition : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for(var hit in colliders)
{
hit.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
if(hit.rigidbody)
{
hit.rigidbody.AddExplosionForce(power, explosionPosition, explosionRadius, power);
}
}
if(particleEffect)
{
var go : GameObject = Instantiate(particleEffect, transform.position, transform.rotation);
}
if(sound != null)
{
audioSource.PlayClipAtPoint(sound, explosionPosition);
}
Destroy(gameObject);
}
Answer by DigitalPowers · Jun 26, 2011 at 03:20 PM
I would say this line is the problem.
if(col.gameObject.tag != "Enemy")
Explode();
You are checking in oncollision if it is not the enemy, then explode. try == instead of !=
Naw its easy to overlook a samll operator, often times I run into problems like that because at first I was approaching it from one direction then I change my mrthod an forget to change the operator