- Home /
How to check against the object type of an instance
Ok so I have a bullet that is fired out of my space ship and then replaces itself with an explosion when it hits something (2D game so when it passes over an object). The problem is it is also exploding when it hits other bullets and explosions which would be cool option but not what I am looking for right now. So I tried to set it up in my script here:
#pragma strict
var ProjectileSpeed : float = 10;
var ProjectileLife : float = 5;
var ImpactType : GameObject;
var owner : GameObject;
var canHitSelf = false;
var canHitSplode = false;
function Start ()
{
rigidbody.velocity += transform.forward * (ProjectileSpeed * 10 * GetComponent(Rigidbody).mass);
var locVel = transform.InverseTransformDirection(rigidbody.velocity);
locVel.x = 0;
rigidbody.velocity = transform.TransformDirection(locVel);
}
function Update ()
{
Destroy(gameObject, ProjectileLife);
var HitObject : GameObject;
//create ray from mouse and... yeah actualy I dont know how this works.
var ray = new Ray (transform.position, Vector3.down);
//Used to store what we hit
var hitInfo : RaycastHit;
//use ray to find object
//Physics.Raycast(ray.origin, ray.direction, hitInfo);
if(Physics.Raycast(ray.origin, ray.direction, hitInfo))
{
//get the colliders parent which should be our hit object
HitObject = hitInfo.collider.gameObject;
if(HitObject != owner)
{
if(canHitSelf == false && HitObject != gameObject || canHitSelf == true)
{
if(canHitSplode == false && HitObject != ImpactType || canHitSplode == true)
{
//Create explosion
Instantiate(ImpactType, transform.position, transform.rotation);
//Get rid of bullet
Destroy(gameObject);
}
}
}
}
}
This had no effect. What I think is happening is that the if statement is looking for the specific instance specified rather than the type of object (e.g. bullet and impact), however, I don't know how I would reference the type rather than the instance. I would also rather not have to add more variables if I could help it, for example I am lookig for something along the lines of a "HitObject != ImpactType.ObjectType" but if that simply is not possible I understand.
Thanks in advance for the help.
You could just set the bullets and explosions to the Ignore Raycast layer, and then they will be ignored by all raycasting and rays will just pass through them.
It's not ideal as you won't be able to shoot at bullets at all (unless using collisions).
Answer by Jamora · Aug 02, 2013 at 07:42 AM
Your if clauses are quite peculiar:
if( canHitSelf == false && HitObject != gameObject || canHitSelf == true)
true/false true/true false/true
What I'm getting at is that this particular if will always return true.
As for your original problem. I think you should have a script, say ExplodableObjectScript
in each object that you want your bullets to explode on. Then, you could check
if (HitObject.GetComponent< ExplodableObjectScript >() != null)
//explode
Although depending on your project's needs, a simple tag could also suffice. In which case you could check
if (HitObject.tag.Equals(ExplodableObject))
//explode
Yeah I realized after I posted that that first if statement was broken. But again when I wrote the code I was thinking it would look at gameObject to mean the type of gameObject not the specific instance of the game object.