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 MicDaRoc · Jul 01, 2015 at 01:19 PM · instantiaterigidbody2dinvoke

Bulletshells not working as intended

Hi! I'm working on a TopDown-Shooter. Shooting works fine. Now I want to implement bullet shells, which pop out of the gun everytime the "Fire"-Button is pressed (it's an Autorifle). I want the shells to fall on the ground, lay there for a certain time and disappear after a while. This is my code so far:

 public float fireRate;
     private float nextFire;
     private GameObject InstantiatedObj;
     public GameObject shot;
     public Transform shotSpawn;
     public Transform MuzzleFlashPrefab;
     public Transform shellEjectionPoint;
     private GameObject InstantiatedShell;
     
 
     void Update () {
     
             string animString = animNumber.ToString ();
             player = GameObject.FindGameObjectWithTag ("Player" + animString);
 
             //shooting
             if (Input.GetButton ("Fire_P" + animString) && Time.time > nextFire) {
                 nextFire = Time.time + fireRate;
                 InstantiatedObj = (GameObject)Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
                 Destroy (InstantiatedObj, 3f);
 
                 //Muzzle Flash
                 Transform clone = Instantiate (MuzzleFlashPrefab, shotSpawn.position, shotSpawn.rotation) as Transform;
                 clone.parent = shotSpawn;
                 float position = Random.Range (0.0f, 0.0f);
                 float size = Random.Range (0.9f, 1.2f);
                 clone.localScale = new Vector3 (size, size, size);
                 clone.localPosition = new Vector3 (position, 0.4f, position);
                 Destroy (clone.gameObject, 0.1f);
 
                 //Shells                
                 InstantiatedShell = (GameObject)Instantiate (shell, shellEjectionPoint.position, Quaternion.identity);
                 InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));
     
                 Invoke ("laydown", 1f);
                 Destroy (InstantiatedShell, 3.0f);
     
     
             }
 }


"laydown" puts the Rigidbody2D to sleep:

 void laydown(){
         InstantiatedShell.GetComponent<Rigidbody2D> ().Sleep ();
 
     }
 

What I got is this: Everytime the Fire button is pushed ONE shell flies out of the gun (in fact out of the screen...), every other shell after the first one is just created with no gravity and stays there for a certain time, before it's destroyed. What I want is that every bullet flies out of the gun in an arc, stays there for a couple of seconds and disappears.

Could anyone be so gentle and help me please? Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dave-Carlile · Jul 01, 2015 at 01:23 PM

The problem is your InstantiatedShell variable. It's global to the class, so every time you're instantiating a new shell, that variable references that latest one. So when your laydown function is called 3 seconds later, its putting to sleep the last shell that was created.

You can fix the scoping problem by using a local variable instead of a class variable...

 GameObject InstantiatedShell = (GameObject)Instantiate(shell, shellEjectionPoint.position, Quaternion.identity);
              InstantiatedShell.GetComponent<Rigidbody2D> ().AddForce (-shellEjectionPoint.forward * Random.Range (180f, 220f));
  

This is declaring the InstantiatedShell variable locally. The problem now becomes, how do you put that object to sleep after 1 second?

First, consider if you even need your laydown function? Doesn't a physics object sleep on its own after awhile once it stops moving?

If you still do, your best bet is to probably add a script on the shell gameobject itself. Have it track the amount of time that has passed since it was created, and then go to sleep.

 float timer;

 void Update()
 {
   if (timer < 0) return;  // nothing to do if we're sleeping

   // otherwise track the time
   timer += Time.deltaTime;

   // go to sleep if we've reached our sleep threshold
   if (timer >= 1.0f)
   {
      GetComponent<RigidBody2D>().Sleep();
      timer = -1;  // stop tracking time
   }
 }

Turn that into a full Monobehaviour and attach it to your bullet object. You can put the Destroy code in there as well and have the object destroy itself after 3 seconds.

Comment
Add comment · Show 1 · 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 MicDaRoc · Jul 07, 2015 at 09:06 AM 0
Share

Hi! Thanks for the reply! I couldn't be on my computer the past few days. Arrived today from a trip, saw your suggestion, implemented it and it works like a charm! Thanks a lot!

avatar image
1

Answer by LaneFox · Jul 01, 2015 at 01:29 PM

I suggest using a Particle System.

Make it emit a Mesh with the material you want on the shell, configure the physics the way you want, set it up on Burst style, point it in the direction you want, keep a reference to it and every time you fire the weapon do shellSystem.Emit(1) (docs).

Works great =)

Comment
Add comment · Show 1 · 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 MicDaRoc · Jul 07, 2015 at 09:13 AM 0
Share

Thanks for your reply! I'm new to Unity and haven't been brave enough to work with particles. But thanks for the hint!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

InvokeRepeating affecting parts of script 1 Answer

Draw a line and instantiate it as a game object 0 Answers

Checking if object intersects? 1 Answer

Variable isn't assigned, even though I have assigned it in the Start() method 2 Answers

When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers


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