Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JtheSpaceC · Oct 07, 2014 at 03:46 PM · bulletsstatistics

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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).

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JtheSpaceC · Oct 07, 2014 at 05:02 PM 0
Share

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?

avatar image Superrodan · Oct 07, 2014 at 10:57 PM 1
Share

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.

avatar image Kiwasi · Oct 08, 2014 at 05:41 AM 0
Share

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.

avatar image JtheSpaceC · Oct 08, 2014 at 01:29 PM 0
Share

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;
         }
 
avatar image robertbu · Oct 08, 2014 at 07:27 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Does anyone have figures which show the take-up ratio for new users hitting unity webplayers for the first time? 2 Answers

Creating Unity Statistics Plugin 0 Answers

Disabled Analytic and HW Statistic but Unity still use cdp.cloud.unity3d.com 1 Answer

Adding movement speed of Player to 1 Answer

How do I make an object disappear when a bullet hits it? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges