- Home /
Make a bullet know who fired it
Hi. So I've a 2d top down space shooter with player, friendly, and enemy fighters zipping around. I have two types of bullets (prefabs). They're identical copies of each other except for the coloring. One type is used by friendlies, one by enemies.
The bullets are all pooled and recycled, so are just setActive when a ship fires them.
How do I get the bullet to know WHO fired it, because I need to set kills of the player and each of his allies, yet they all use the same bullets. Same situation with the enemies.
I was thinking something like OnEnable, I do some sort of circular raycast (or a straight one aimed behind) to find the nearest game object, because that MUST be the one who fired it, in theory. I just don't know enough about this area to succeed.
Can anyone provide a sample code, or even a simpler, more efficient solution to the problem?
Thanks a mil -Kevin
Answer by robertbu · Oct 07, 2014 at 03:50 PM
The 'right' answer is to have the object that fired the bullet to tell the bullet. You can do this by changing the bullets name or its tag. If you have a script on the bullet, you can store the information in a component and use either SendMessage() or directly accessing the component to set the value.
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://unitygems.com/script-interaction1/
http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/
And if you really want to do your 'raycasting thing', you could do it by calling Physics.OverlapSphere() and then sorting through the results to find an object of the time that could have fired the bullet (*but I don't recommend this solution).
Thanks a mil for pointing me in the right direction. I have the desired effect now but I keep getting this console error.
Script error ($$anonymous$$over): OnEnable() can not take parameters.
$$anonymous$$y $$anonymous$$over script is on each of the bullets and is receiver the parameter of "theFirer" from the script on the ship that fires the bullets. $$anonymous$$over.cs looks like this:
public float shotSpeed;
public static float bulletSpeed;
void OnEnable (GameObject theFirer)
{
audio.Play ();
rigidbody2D.velocity = theFirer.rigidbody2D.velocity;
rigidbody2D.AddForce (transform.up * shotSpeed/100);
}
The error just appears once in Unity even before I try to play. It doesn't stop the game runner or spoil the effect. Anyone know what the error is about?
This is just a guess, but I believe the error is because OnEnable is a script built into Unity as opposed to one you wrote.
It's not supposed to have any parameters passed into it. If you rename the script to something like "void FiredShot" or something I believe the error would go away.
Let me translate the answer into code for you, since you missed the point rather dramatically.
// On the shooter
GameObject bullet = (GameObject)Instantiate (bulletPrefab);
bullet.GetComponent<BulletScript>().firer = gameObject;
// On the bullet
public GameObject firer;
void OnEnable (){
// Do whatever
}
This is pseudo code. It will have to be modified to suit your code.
Thanks for that code. I can see how the message can be sent, whereas I didn't know before. However, I'm using object pooling on the bullets so they're enabled at game start and return errors that the game object 'firer' hasn't been assigned yet. I added a check to only seek the gameobject velocity if the GameObject isn't null. See below.
public GameObject theFirer = null;
void OnEnable ()
{
audio.Play ();
if(theFirer != null)
{
rigidbody2D.velocity = theFirer.rigidbody2D.velocity;
rigidbody2D.AddForce (transform.up * shotSpeed/100);
}
}
However this results in very strange behaviour. It doesn't return the error, but the bullets only find their firer half the time, and on the ones that didn't find the firer, their world velocity it just zero. Here's the script on the bulletFireScript:
public void Fire ()
{
GameObject obj1 = shotPoolerScript.current.GetPooledObject();
obj1.transform.position = shotSpawn1.position;
obj1.transform.rotation = shotSpawn1.rotation;
obj1.SetActive(true);
obj1.GetComponent<$$anonymous$$over>().theFirer = this.gameObject;
GameObject obj2 = shotPoolerScript.current.GetPooledObject();
obj2.transform.position = shotSpawn2.position;
obj2.transform.rotation = shotSpawn2.rotation;
obj2.SetActive(true);
obj1.GetComponent<$$anonymous$$over>().theFirer = this.gameObject;
}
I see nothing wrong with this code. In this line:
if (theFirer != null)
'theFirer' will be null if the game object originally assigned is destroyed. Not sure how your game works. In addition, I'd put a Debug.Log() in the 'else' so that you get output if 'theFirer' is null.