Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gpopcorn · Feb 01, 2020 at 03:41 AM · c#3dwebgllag

Lag When Built With WebGL (Link for examining the problem) Updated! Please help!

Here is the link for my game so you can see if you can identify the problem:

https://chillpillgames.itch.io/flying-balls

So my project is very small (less than 10mb), but there is major lag when I build it. The game was very slow and laggy after I built it, but in the editor it was perfectly fine. I tried Itch.io and a localhost but they both had the same result. Here are my code samples if they help:

Sphere Spawner:

 using UnityEngine;
 
 public class EnemySpawnScript : MonoBehaviour
 {
 
     public GameObject enemyPrefab;
     private float amountToSpawn = 2000;
 
     private void Update()
     {
         if(amountToSpawn > 0)
         {
             Instantiate(enemyPrefab, new Vector3(Random.Range(-199, 199), Random.Range(-99, 99), 0), Quaternion.identity);
             amountToSpawn -= 1f;
         }
     }
 
 }

Movement:

 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
 
     public float thrust = 1000f;
     public Rigidbody rb;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
     private void Update()
     {
         if (Input.GetKeyUp("w"))
         {
             rb.AddForce(0, thrust, 0);
         }
         if (Input.GetKeyUp("s"))
         {
             rb.AddForce(0, -thrust, 0);
         }
         if (Input.GetKeyUp("a"))
         {
             rb.AddForce(-thrust, 0, 0);
         }
         if (Input.GetKeyUp("d"))
         {
             rb.AddForce(thrust, 0, 0);
         }
     }
 
 }

Collision:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerCollisionScript : MonoBehaviour
 {
     public Text finalScoreText;
     public Text scoreText;
     private int score = 0;
 
     private void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "Enemy")
         {
             Destroy(collision.gameObject);
             score += 1000;
             scoreText.text = "Score: " + score.ToString();
             finalScoreText.text = "Score: " + score.ToString();
             SliderScript.juiceLeft += 3;
         }
     }
 
 }


I really need help and also I need the demo build finished by 2 days from now, and anything you say can definitely help me. Also if you have any ideas for the game that would be great!

Comment
Add comment · Show 3
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 Bunny83 · Feb 03, 2020 at 06:33 AM 1
Share

We can't really help you here since you haven't provided any useful information. I tested your WebGL build and yes, you have some serious input delay. Since we have no idea how you read your input, how you setup your input (input manager? sensitivity? gravity? of your inputs) or how you move your sphere around there's nothing we can base an answer on. Next thing is instantiating spheres is quite expensive and pretty unnecessary for a 2d game. A sphere has a couple of hundreds of vertices and triangles.


Try some of my webGL examples. I don't really have games up there but just some examples. However those which have direct keyboard input are my VisPortals test and my simple towers of hanoi game (keys 1,2 and 3). You probably are using the physics engine for your collision detection. While this is possible, using the Physics2D engine and proper 2d colliders would be most likely better for performance. Though I don't think that rendering performance is an actual issue here. We need to see the code that actually moves your sphere.


Note that if you plan to have that many "spheres" / dots on the screen you might want to use Unity particle system. I made this example years ago. It simply reads in an image with the unity logo and creates a particle for each non transparent pixel. When you hold down the mouse button you create a repulsive spherical force around your mouse. The particles are forced back to their resting position every frame. Note that I have 23000+ particles in that example. Doing this with gameobjects would completely kill the game.


$$anonymous$$eep in $$anonymous$$d that WebGL generally has very little memory available. By default only 256$$anonymous$$B. A gameobject is quite large and rather expensive if you have many of them.


Please edit your question and add relevant details about your game. Especially your input settings / input method and the code that actually moves your sphere.

avatar image Gpopcorn Bunny83 · Feb 03, 2020 at 05:32 PM 0
Share

I did 3d because I don’t know how to work 2d physics, collision, movement, and layers and it would take me long to figure out. This project is just something I want to start with so that I can build off of later. The memory of my entire game is less than 10mb so I don’t see how that could be a problem. I don’t know how to work with a particle system and it’s not something that I want to spend an hour to learn. I know the basics but not the things that you do.

avatar image Gpopcorn Bunny83 · Feb 03, 2020 at 07:40 PM 0
Share

@Bunny83 I updated the question. Hope this helps!

2 Replies

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

Answer by Bunny83 · Feb 04, 2020 at 02:13 AM

Now that we see your code it's pretty obvious what is wrong. The biggest issue is that you use "GetKeyUp". This will only be true for one frame when you release your key. So pressing down one of your buttons will have no effect at all. However whenever you release one of your keys you apply a one-time force.


If you really want your control to be one-time impulses so the user has to constantly tapping the movement buttons, you should use GetKeyDown instead of GetKeyUp. This will react to a key down event instead of a key up event.


Though if you want to accelerate into the direction while the user presses a key down, you should use GetKey instead of GetKeyDown / Up.


Finally you use AddForce wrong. AddForce with ForceMode.Force (the default) expects a force in Newton which should be applied every frame over time. If you want to apply a one-time impulse you should use ForceMode.Impulse. Though if you use GetKey and you apply a force every frame while a button is pressed down, you should use ForceMode.Force. Note that your force value need to be much smaller. Currently you would apply 1000 Newtons (1000 kg*m/s²). So a mass of 1kg would be accelerated to 1000 m/s every second. A value of about 20 would make more sense. Keep in mind since you made your force variable public, changing the field initializer in your code from 1000 to 20 has no effect since the value is serialized in the inspector. So you have to change the value in the inspector.

 // impulse logic as explained above
 private void Update()
 {
     if (Input.GetKeyDown("w"))
         rb.AddForce(0, thrust, 0, ForceMode.Impulse);
     if (Input.GetKeyDown("s"))
         rb.AddForce(0, -thrust, 0, ForceMode.Impulse);
     if (Input.GetKeyDown("a"))
         rb.AddForce(-thrust, 0, 0, ForceMode.Impulse);
     if (Input.GetKeyDown("d"))
         rb.AddForce(thrust, 0, 0, ForceMode.Impulse);
 }

If you want to accelerate continuously use something like that:

 private void FixedUpdate()
 {
     if (Input.GetKey("w"))
         rb.AddForce(0, thrust, 0);
     if (Input.GetKey("s"))
         rb.AddForce(0, -thrust, 0);
     if (Input.GetKey("a"))
         rb.AddForce(-thrust, 0, 0);
     if (Input.GetKey("d"))
         rb.AddForce(thrust, 0, 0);
 }
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 Gpopcorn · Feb 04, 2020 at 02:38 AM 0
Share

Thnak you so veery mych!

avatar image
0

Answer by Nk_Khumbhani · Feb 03, 2020 at 11:55 AM

I think you should use invokeRepeating to spawn spheres in parts or use object pulling concept.

Comment
Add comment · Show 2 · 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 Gpopcorn · Feb 03, 2020 at 05:19 PM 0
Share

I will try this! Thanks!

avatar image Gpopcorn Gpopcorn · Feb 03, 2020 at 07:05 PM 0
Share

This did not work and I'm sad.

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

163 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to reduce Draw-Calls? 1 Answer

WebGL Build bugged Colliders 0 Answers

Gun not smoothly following player,How to make gun smoothly follow player 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