• 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 /
  • Help Room /
avatar image
Question by Trioxide1997 · Jan 12, 2016 at 07:16 AM · optimization

How to optimize a lot of bullets?

The game I'm working on has over 100 bullets at chaotic times and it gets really laggy. The enemies instantiate them when they spawn. Attached to each is a mover script, destroyOnContact script, rigidbody2D, circle collider 2D and sprite renderer.

Could you help me find out whats the main cause of this problem? I was assuming it was the rigidbody but I can't find a way to move the bullets without it.

edit: Here is the code, changed it up to get rid of rigidbody which helped but still a little bit laggy on my phone.

 using UnityEngine;
 using System.Collections;
 
 public class Mover : MonoBehaviour {
 
     public float speed;
     /*private Rigidbody2D rb;
 
     void Start ()
     {
         rb = GetComponent<Rigidbody2D> ();
         rb.AddForce(transform.right * speed);
     }*/
 
     public Vector3 Velocity;
 
     void Update()
     {
         transform.Translate(Velocity * Time.deltaTime * speed);    
     }
 }
Comment

People who like this

0 Show 2
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 JedBeryll · Jan 12, 2016 at 07:38 AM 0
Share

If you would show us the bullet mover script and the code that spawns them it would help a lot. Other than that my advice is try to give every variable a starting value and save the reference to the bullet's rigidbody in the mover script when you save it to a prefab so when you instantiate the bullet, you don't have to use GetComponents. Also if there are values that are the same for each bullet make those constant (for less memory usage).

avatar image Trioxide1997 JedBeryll · Jan 12, 2016 at 07:55 AM 0
Share

All right edited, thank you for the suggestion about not having to use GetComponent.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by JedBeryll · Jan 12, 2016 at 08:15 AM

This isn't exactly what i had in mind. This is ok but if the game lags there could be side effects like the bullet skipping the target. Rigidbodies are great for avoiding this. So i think you should keep the rigidbody but assign it in the inspector before saving the bullet as prefab. Just drag and drop the Bullet object on its own script's Rb. Another thing: "transform" in a script as the same as GetComponent(). So either use Vector2.right (this would be world space right), or save the transform too in the same way.

public class Mover : MonoBehaviour {

  public float speed; //if this is always the same: public const float speed = 5;   for example.
  public Rigidbody2D rb;
 
  void Start ()
  {
      rb.AddForce(Vector2.right * speed, ForceMode2D.Impulse);
  }
 

[1]: /storage/temp/61777-bullet.png


bullet.png (36.4 kB)
Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

37 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

same material for different texture ? to increase performance 1 Answer

Physics2D Optimize 1 Answer

How do I reduce build time in-editor? 0 Answers

Why is Vector3.zero slower than new Vector3(0,0,0)? 2 Answers

Game works better as Windows Application than in WebGL. Why? 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