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 Bombshell93 · Aug 05, 2012 at 04:28 AM · raycastshootingbulletbatchingefficiency

How should I handle masses of bullets?

I'm making a top down shooter and I face a problem which is significant slow down (320fps down to 140fps) when firing an infinite clip automatic weapon.

the bullet at the moment is a cylinder of 18 polys (easily lowered) and I noticed not a single one of them is batched, if that is the issue, how could I get this to work?

Another thing that may be the issue is on firing it creates a new bullet prefab which on creation gets its rotation, does a raycast as far as its range, sets the bullet length (stopping it when it hits a wall and stopping it at its range if it does not) the materials alpha is then decreased for a second until the bullets alpha is less than or equal to zero at which point it destroys its containing object.

Is this poor practice? if so how can I improve its efficiency? I'd like to make it as fast as possible so it can run on lower end machines.

Any and all help is greatly appreciated,

Thanks in advanced, Bombshell

Comment
Add comment · Show 4
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 Seth-Bergman · Aug 05, 2012 at 05:34 AM 0
Share

I would start by removing the bullet code, and the alpha material code, in turn, just to see how they affect framerate individually, to pinpoint the issue.. I assume there is not an outrageous number of bullets on the screen, so my best guess is either you've something in the Update function that should be in the Start, or maybe the alpha channel part is the cause

avatar image Bombshell93 · Aug 05, 2012 at 06:33 AM 0
Share

I just removed all the initial code and only left the countdown to the object end. I also changed the shader from a custom alpha Fresnel shader to the unlit/texture. I gain 20 FPS but that means I've still 130 or so FPS, the amount alive at any one time with maximum rate of fire and the current bullet life time, about 50. I think the problem is the creating and destroying of those objects in so short a time frame, but I'm not sure how I'd fix that.

avatar image Seth-Bergman · Aug 05, 2012 at 07:17 AM 0
Share

well, can you actually see the bullets? do you need to be able to? just doing a direct raycast without instantiating so many bullets would probably do it. seems like they must be going pretty fast if there're that many.. maybe you could just do fewer or none actual objects, and just raycast from the weapon itself

avatar image Bombshell93 · Aug 05, 2012 at 07:48 AM 0
Share

the bullets dont really move, they're there so you can see the shot mainly, the many raycast could easily be done in firing object but I dont know how I would draw the various shots without them rotating with the firing object. Hence why I made their own objects to host the bullets, so they could have independent transforms.

1 Reply

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

Answer by Leocesar · Aug 05, 2012 at 07:57 AM

You must use object pooling for the projectiles.

In a nutshell it instantiate all the bullets when the scene loads, and then it reuses those GameObjets over and over again without the need to instantiate new ones.

Here is the code I wrote to tackle such issue:

ObjectPooling.cs (Attach it to an empty GameObject):

 using UnityEngine;
 using System.Collections;
 
 public class ObjectPooling : MonoBehaviour
 {
  public GameObject prefab;
  public int poolSize;
 
  private GameObject[] pool;
 
  void Start ()
  {
  pool = new GameObject[poolSize];
  
  for (int i = 0; i < poolSize; i++) {
  pool[i] = (GameObject)Instantiate (prefab, transform.position, Quaternion.identity);
  pool[i].SetActiveRecursively (false);
  }
  }
  
  public GameObject RetrieveInstance ()
  {
  foreach (GameObject go in pool) {
  if (!go.active) {
  go.SetActiveRecursively(true);
  return go;
  } 
  }
  
  return null;
  }
 
  public void DevolveInstance (GameObject go)
  {
  go.SetActiveRecursively (false);
  }
 }

SpawnPool.cs (Attach it to a GameObject that will spawn the bullets, in your case the ship or soldier holding the gun):

 using UnityEngine;
 using System.Collections;
 
 public class SpawnPool : MonoBehaviour
 {
  private ObjectPooling pool;
  private GameObject go;
  private int contador = 2;
 
  void Start ()
  {
  pool = GameObject.FindGameObjectWithTag ("BulletPool").GetComponent<ObjectPooling> ();
  }
 
  void Update ()
  {
  //Debug.Log(contador);
  //contador++;
  
  //if (contador > 2) {
  go = pool.RetrieveInstance ();
  
  if (go) {
  go.transform.position = transform.position;
  }
  
  // contador = 0;
  //}
  }
 }

AutoDevolvePool.cs (Attach it to a bullet Prefab):

 using UnityEngine;
 using System.Collections;
 
 public class AutoDevolvePool : MonoBehaviour
 {
  public int time = 2;
 
  private float seconds = 0;
  private ObjectPooling pooling;
 
  void Start ()
  {
  pooling = GameObject.FindGameObjectWithTag ("BulletPool").GetComponent<ObjectPooling> ();
  }
 
  void Update ()
  {
  seconds += Time.deltaTime;
  
  if (seconds >= time) {
  //Debug.Log("devolvido");
  pooling.DevolveInstance (gameObject);
  seconds = 0;
  }
  }
 }

Hope that helps!

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 Bombshell93 · Aug 05, 2012 at 07:21 PM 1
Share

it did help plenty by cleaning up my code and saving another 20FPS but the FPS drop still occurred. So I looked into how the gun was handled and there were some issues with rate of fire being limitless which was a drain of 40-60FPS, I'm pretty much back to optimal now.

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycasr in my fps? 1 Answer

Best way to shoot physical bullets? 2 Answers

How to get bullets to hit crosshair 2 Answers

Random Raycast inside crosshairs 1 Answer

shooting problem with raycasting 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